generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.kt
36 lines (31 loc) · 997 Bytes
/
Day02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private val String.dir get() = substringBefore(' ')
private val String.step get() = substringAfter(' ').toInt()
fun main() {
fun part1(input: List<String>): Int {
val commands = input.groupBy(String::dir, String::step)
val x = commands["forward"]!!.sum()
val y = commands["down"]!!.sum() - commands["up"]!!.sum()
return x * y
}
fun part2(input: List<String>): Int {
var (x, y, aim) = Triple(0, 0, 0)
input.forEach {
val (dir, step) = it.dir to it.step
when (dir) {
"forward" -> {
x += step
y += aim * step
}
"up" -> aim -= step
"down" -> aim += step
}
}
return x * y
}
val testInput = readInput("Day02_test")
check(part1(testInput) == 150)
check(part2(testInput) == 900)
val input = readInput("Day02")
println(part1(input))
println(part2(input))
}