1
0

Day02 2021

This commit is contained in:
Hubert Van De Walle 2021-12-02 09:08:06 +01:00
parent 15b11a191d
commit b9a8e7585b
3 changed files with 1058 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Text
@Day
class Day02(@Text input: String) {
private val lines = input.lines().map { it.split(' ').let { it[0] to it[1].toInt() } }
fun part1(): Any {
var horizontalPosition = 0
var depth = 0
lines.forEach { (direction, value) ->
when (direction) {
"forward" -> horizontalPosition += value
"down" -> depth += value
"up" -> depth -= value
}
}
return horizontalPosition * depth
}
fun part2(): Any {
var horizontalPosition = 0
var depth = 0
var aim = 0
lines.forEach { (direction, value) ->
when (direction) {
"forward" -> {
horizontalPosition += value
depth += aim * value
}
"down" -> aim += value
"up" -> aim -= value
}
}
return horizontalPosition * depth
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
package be.vandewalleh.aoc.days
class Day02Test : BaseDayTest(2) {
override val example = """
forward 5
down 5
forward 8
up 3
down 8
forward 2
""".trimIndent()
override val part1Example = 150
override val part2Example = 900
override val part1Answer = 1938402
override val part2Answer = 1947878632
}