diff --git a/2021/02/main.kt b/2021/02/main.kt index 16e601d..318570a 100644 --- a/2021/02/main.kt +++ b/2021/02/main.kt @@ -4,6 +4,16 @@ fun main() { val lines = File("input/1.txt").readLines() val directions = lines.map { line -> line.split(' ', limit = 2).let { it[0] to it[1].toInt() } } + println("Part 1:") + part1(directions) + + println() + + println("Part 2:") + part2(directions) +} + +private fun part1(directions: List<Pair<String, Int>>) { var position = 0 var depth = 0 @@ -19,3 +29,24 @@ fun main() { println("Depth: $depth") println("Multiplied: ${position * depth}") } + +private fun part2(directions: List<Pair<String, Int>>) { + var position = 0 + var depth = 0 + var aim = 0 + + for ((direction, distance) in directions) { + when (direction) { + "forward" -> { + position += distance + depth += aim * distance + } + "up" -> aim -= distance + "down" -> aim += distance + } + } + + println("Position: $position") + println("Depth: $depth") + println("Multiplied: ${position * depth}") +} diff --git a/README.md b/README.md index 621689e..3a04e9f 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,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 |