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

Add 2021 - Day 2 - Part 2

This commit is contained in:
chylex 2021-12-03 06:29:04 +01:00
parent 457d8ef8a8
commit 8004a505e4
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 32 additions and 0 deletions

View File

@ -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}")
}

View File

@ -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 |