-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.kt
29 lines (25 loc) · 905 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
data class Coordinates(val horizontal: Int, val depth: Int, val aim: Int)
private fun part1(moves: List<Pair<Int, Int>>): Int {
val result = moves.reduce { a, b -> a.first + b.first to a.second + b.second }
return result.first * result.second
}
private fun part2(moves: List<Pair<Int, Int>>): Int {
val result = moves.fold(Coordinates(0, 0, 0)) { (horizontal, depth, aim), (x, y) ->
Coordinates(horizontal + x, depth + x * aim, aim + y)
}
return result.horizontal * result.depth
}
fun main() {
val moves = mapLines {
val (direction, distanceString) = it.split(" ")
val distance = distanceString.toInt()
when (direction) {
"forward" -> distance to 0
"up" -> 0 to -distance
"down" -> 0 to distance
else -> error("Illegal input")
}
}
println(part1(moves))
println(part2(moves))
}