-
Notifications
You must be signed in to change notification settings - Fork 4
/
Day10.kt
63 lines (57 loc) · 1.81 KB
/
Day10.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
53
54
55
56
57
58
59
60
61
62
63
package com.github.ephemient.aoc2022
@Day
class Day10(lines: List<String>) {
private val instructions = lines.map { it.toInstruction() }
@Day.Part
fun part1(): Int {
val iterator = instructions.iterator()
var cycle = 0
var x = 1
return taps.sumOf { i ->
var lastX = x
while (cycle < i) {
lastX = x
when (val instruction = iterator.next()) {
Instruction.Noop -> cycle++
is Instruction.AddX -> {
cycle += 2
x += instruction.dx
}
}
}
i * lastX
}
}
@Day.Part
fun part2(): String = buildString(245) {
val iterator = instructions.iterator()
var cycle = 0
var x = 1
for (row in 0 until 240 step 40) {
if (row != 0) append('\n')
repeat(40) { col ->
append(if (x - col in -1..1) '\u2593' else '\u2591')
while (cycle < row + col) {
when (val instruction = iterator.next()) {
Instruction.Noop -> cycle++
is Instruction.AddX -> {
cycle += 2
x += instruction.dx
}
}
}
}
}
}
private sealed class Instruction {
object Noop : Instruction()
data class AddX(val dx: Int) : Instruction()
}
companion object {
private val taps get() = intArrayOf(20, 60, 100, 140, 180, 220)
private fun String.toInstruction(): Instruction = when {
this == "noop" -> Instruction.Noop
else -> Instruction.AddX(removePrefix("addx ").toInt())
}
}
}