1
0
mirror of https://github.com/chylex/Advent-of-Code.git synced 2025-05-16 01:34:04 +02:00

Add 2021 - Day 3 - Part 1

This commit is contained in:
chylex 2021-12-03 06:37:06 +01:00
parent 8004a505e4
commit d6aa3ac901
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
5 changed files with 1032 additions and 0 deletions

View File

@ -0,0 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2021 - Day 03" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="MainKt" />
<module name="Advent_of_Code.2021-03" />
<shortenClasspath name="NONE" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/2021/03" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

1000
2021/03/input/1.txt Normal file

File diff suppressed because it is too large Load Diff

19
2021/03/main.kt Normal file
View File

@ -0,0 +1,19 @@
import java.io.File
fun main() {
val lines = File("input/1.txt").readLines().filter(String::isNotEmpty)
val totalRows = lines.size
val totalColumns = lines[0].length
val totalOnesPerColumn = Array(totalColumns) { column ->
lines.count { it[column] == '1' }
}
val gammaRate = totalOnesPerColumn
.map { if (it > totalRows / 2) 1 else 0 }
.foldIndexed(0) { index, total, bit -> total or (bit shl (totalColumns - index - 1)) }
val epsilonRate = gammaRate xor ((1 shl totalColumns) - 1)
println("Power consumption: ${gammaRate * epsilonRate}")
}

View File

@ -32,3 +32,4 @@ The versions should not matter, but I used Visual Studio 2019 with `MSVC v142 (1
| 2015 | 01 | NASM x86 |
| 2021 | 01 | Kotlin |
| 2021 | 02 | Kotlin |
| 2021 | 03 | Kotlin |

View File

@ -34,4 +34,5 @@ sourceSets {
make(2021, 1)
make(2021, 2)
make(2021, 3)
}