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
48 lines (43 loc) · 1.3 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
fun main() {
fun part1(commands: List<Command>): Int {
var position = 0
var depth = 0
commands.forEach {
when (it.type) {
"forward" -> position += it.movement
"down" -> depth += it.movement
"up" -> depth -= it.movement
}
}
return position * depth
}
fun part2(commands: List<Command>): Int {
var position = 0
var aim = 0
var depth = 0
commands.forEach {
when (it.type) {
"forward" -> {
position += it.movement
depth += aim * it.movement
}
"down" -> aim += it.movement
"up" -> aim -= it.movement
}
}
return position * depth
}
// test if implementation meets criteria from the description
val testInput = readInput("Day02_test").toCommands()
check(part1(testInput) == 150)
check(part2(testInput) == 900)
val input = readInput("Day02").toCommands()
println(part1(input))
println(part2(input))
}
private fun List<String>.toCommands(): List<Command> = this.map {
with (it.split(" ")) {
Command(this[0], this[1].toInt())
}
}
data class Command(val type: String, val movement: Int)