-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.kt
More file actions
46 lines (39 loc) 路 1.17 KB
/
Copy pathDay10.kt
File metadata and controls
46 lines (39 loc) 路 1.17 KB
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
private fun onInstructionIteration(instructions: List<Int?>, action: (i: Int, x: Int) -> Unit) {
instructions.fold(1 to 0) { pair, addV ->
var (x, i) = pair
action(i, x)
if (addV != null) {
i += 1
action(i, x)
x += addV
}
x to i + 1
}
}
private fun part1(instructions: List<Int?>): Int {
var sum = 0
onInstructionIteration(instructions) { i, x ->
if (i + 1 == 20 || (i + 1 - 20) % 40 == 0) {
sum += (i + 1) * x
}
}
return sum
}
private fun part2(instructions: List<Int?>): String {
val pixels = mutableSetOf<Pair<Int, Int>>()
val width = 40
onInstructionIteration(instructions) { i, x ->
if ((i % width) + 1 in x..x + 2) {
pixels += i / width to i % width
}
}
val levels = pixels.maxOf { it.first }
return (0..levels).joinToString("\n") { i ->
(0 until width).joinToString("") { j -> if (i to j in pixels) "#" else "." }
}
}
fun main() {
val instructions = mapLines { if (it != "noop") it.split(' ')[1].toInt() else null }
println(part1(instructions))
println(part2(instructions))
}