diff --git a/2024/src/main/kotlin/day17.kt b/2024/src/main/kotlin/day17.kt index 35f9007..4a674f6 100644 --- a/2024/src/main/kotlin/day17.kt +++ b/2024/src/main/kotlin/day17.kt @@ -6,8 +6,14 @@ Register C: 0 Program: 2,4,1,3,7,5,1,5,0,3,4,1,5,5,3,0 */ -class Computer(var a: Int = 0, var b: Int = 0, var c: Int = 0, var ip: Int = 0, val memory: List) { - val output = mutableListOf() +class Computer( + var a: Int = 0, + var b: Int = 0, + var c: Int = 0, + private var ip: Int = 0, + val memory: List +) { + private val output = mutableListOf() fun run(): List { ip = 0 @@ -21,6 +27,7 @@ class Computer(var a: Int = 0, var b: Int = 0, var c: Int = 0, var ip: Int = 0, 3 -> if (a != 0) { ip = literalOperand() - 2 } + 4 -> b = b xor c 5 -> output.add(comboOperand() and 7) 6 -> b = a / (1 shl comboOperand()) @@ -32,7 +39,7 @@ class Computer(var a: Int = 0, var b: Int = 0, var c: Int = 0, var ip: Int = 0, return output.toList() } - private fun comboOperand(): Int = when(val value = memory[ip + 1]) { + private fun comboOperand(): Int = when (val value = memory[ip + 1]) { 0, 1, 2, 3 -> value 4 -> a 5 -> b @@ -45,7 +52,7 @@ class Computer(var a: Int = 0, var b: Int = 0, var c: Int = 0, var ip: Int = 0, } fun main() { - val computer = Computer(a = 21539243, memory = listOf(2,4,1,3,7,5,1,5,0,3,4,1,5,5,3,0)) + val computer = Computer(a = 21539243, memory = listOf(2, 4, 1, 3, 7, 5, 1, 5, 0, 3, 4, 1, 5, 5, 3, 0)) val output = computer.run().joinToString(separator = ",") println("Part 1: $output") } \ No newline at end of file