-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay09.kt
executable file
·52 lines (48 loc) · 1.54 KB
/
Day09.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
49
50
51
52
fun main() {
fun part1(input: List<String>): Int {
return input
.map { line -> line.split(" ").map { it.toInt() } }
.sumOf {
val stack = ArrayDeque<Int>()
var currentList = it.toList()
while (currentList.toSet().size != 1) {
stack.add(currentList.last())
val newList = currentList
.mapIndexedNotNull { index, value -> if (index == 0) null else value - currentList[index - 1] }
.toList()
currentList = newList
}
stack.add(currentList.last())
stack.sum()
}
}
fun part2(input: List<String>): Int {
return input
.map { line -> line.split(" ").map { it.toInt() } }
.sumOf {
val dequeue = ArrayDeque<Int>()
var currentList = it.toList()
while (currentList.toSet().size != 1) {
dequeue.add(currentList.first())
val newList = currentList
.mapIndexedNotNull { index, value -> if (index == 0) null else value - currentList[index - 1] }
.toList()
currentList = newList
}
dequeue.add(currentList.first())
var top = dequeue.removeLast()
while (dequeue.isNotEmpty()) {
val next = dequeue.removeLast()
top = next - top
}
top
}
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day09_test")
val input = readInput("Day09")
check(part1(testInput) == 114)
part1(input).println()
check(part2(testInput) == 2)
part2(input).println()
}