Day 5, puzzle 1
This commit is contained in:
107
src/main/kotlin/com/basdado/adventofcode/day5/Day5.kt
Normal file
107
src/main/kotlin/com/basdado/adventofcode/day5/Day5.kt
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.basdado.adventofcode.day5
|
||||
|
||||
import com.basdado.adventofcode.day5.Day5.puzzle1
|
||||
import com.basdado.adventofcode.line
|
||||
|
||||
const val DAY5_INPUT_PATH = "/day/5/input.txt"
|
||||
|
||||
|
||||
fun main() {
|
||||
puzzle1()
|
||||
}
|
||||
|
||||
object Day5 {
|
||||
|
||||
val opcodes = mapOf(
|
||||
Pair(1, OpCode(1, 3, ::add)),
|
||||
Pair(2, OpCode(2, 3, ::multiply)),
|
||||
Pair(3, OpCode(3, 1, ::saveInput)),
|
||||
Pair(4, OpCode(4, 1, ::writeOutput))
|
||||
)
|
||||
|
||||
fun puzzle1() {
|
||||
|
||||
val program = Program(line(DAY5_INPUT_PATH).split(",").map { it.toInt() }.toIntArray(),
|
||||
1)
|
||||
|
||||
var pointer = 0
|
||||
while (program.data[pointer] != 99) {
|
||||
|
||||
println(pointer)
|
||||
|
||||
val instruction = Instruction.parse(program.data[pointer], pointer)
|
||||
instruction.opcode.operation.invoke(instruction, program)
|
||||
|
||||
pointer += instruction.opcode.paramCount + 1
|
||||
}
|
||||
}
|
||||
|
||||
class Instruction(
|
||||
val paramModeImmediate: BooleanArray,
|
||||
val opcode: OpCode,
|
||||
val pos: Int
|
||||
) {
|
||||
|
||||
companion object {
|
||||
fun parse(input: Int, pointer: Int): Instruction {
|
||||
val opcode = opcodes[input % 100] ?: throw IllegalArgumentException("Uknown opcode for operation $input")
|
||||
val paramModes = BooleanArray(opcode.paramCount)
|
||||
for (i in 0 until opcode.paramCount) {
|
||||
paramModes[i] = (input % pow10(i + 3)) / pow10(i + 2) == 1
|
||||
}
|
||||
return Instruction(paramModes, opcode, pointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Program(val data: IntArray, val input: Int) {
|
||||
|
||||
fun getValue(instruction: Instruction, i: Int): Int {
|
||||
return if (instruction.paramModeImmediate[i]) {
|
||||
data[instruction.pos + i + 1]
|
||||
} else {
|
||||
data[data[instruction.pos + i + 1]]
|
||||
}
|
||||
}
|
||||
|
||||
fun setValue(instruction: Instruction, i: Int, x: Int) {
|
||||
// Setters don't care about parameter modes, they are always "position" based
|
||||
data[data[instruction.pos + i + 1]] = x
|
||||
}
|
||||
}
|
||||
|
||||
fun pow10(n: Int): Int {
|
||||
return when (n) {
|
||||
0 -> 1
|
||||
1 -> 10
|
||||
2 -> 100
|
||||
3 -> 1000
|
||||
4 -> 10000
|
||||
5 -> 100000
|
||||
6 -> 1000000
|
||||
7 -> 10000000
|
||||
8 -> 100000000
|
||||
9 -> 1000000000
|
||||
else -> throw IllegalArgumentException("Int overflowing with pow10($n)")
|
||||
}
|
||||
}
|
||||
private fun add(instruction: Instruction, program: Program) {
|
||||
program.setValue(instruction, 2, program.getValue(instruction, 0) + program.getValue(instruction, 1))
|
||||
}
|
||||
|
||||
private fun multiply(instruction: Instruction, program: Program) {
|
||||
program.setValue(instruction, 2, program.getValue(instruction, 0) * program.getValue(instruction, 1))
|
||||
}
|
||||
|
||||
private fun saveInput(instruction: Instruction, program: Program) {
|
||||
program.setValue(instruction, 0, program.input)
|
||||
}
|
||||
|
||||
private fun writeOutput(instruction: Instruction, program: Program) {
|
||||
// I guess we just write to console?
|
||||
println(program.getValue(instruction, 0))
|
||||
}
|
||||
|
||||
data class OpCode(val code: Int, val paramCount: Int, val operation: (instruction: Instruction, program: Program) -> Unit)
|
||||
|
||||
}
|
||||
1
src/main/resources/day/5/input.txt
Normal file
1
src/main/resources/day/5/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
3,225,1,225,6,6,1100,1,238,225,104,0,1002,36,25,224,1001,224,-2100,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1102,31,84,225,1102,29,77,225,1,176,188,224,101,-42,224,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,2,196,183,224,1001,224,-990,224,4,224,1002,223,8,223,101,7,224,224,1,224,223,223,102,14,40,224,101,-1078,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1001,180,64,224,101,-128,224,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,24,17,224,1001,224,-408,224,4,224,1002,223,8,223,101,2,224,224,1,223,224,223,1101,9,66,224,1001,224,-75,224,4,224,1002,223,8,223,1001,224,6,224,1,223,224,223,1102,18,33,225,1101,57,64,225,1102,45,11,225,1101,45,9,225,1101,11,34,225,1102,59,22,225,101,89,191,224,1001,224,-100,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,226,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,108,226,226,224,1002,223,2,223,1006,224,344,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,359,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,374,101,1,223,223,1008,677,226,224,1002,223,2,223,1006,224,389,101,1,223,223,8,677,677,224,1002,223,2,223,1005,224,404,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,419,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,434,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,449,1001,223,1,223,107,677,226,224,1002,223,2,223,1005,224,464,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,479,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,494,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,509,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1007,677,226,224,102,2,223,223,1005,224,539,1001,223,1,223,1107,226,226,224,1002,223,2,223,1006,224,554,1001,223,1,223,1008,226,226,224,1002,223,2,223,1006,224,569,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,584,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,599,1001,223,1,223,1007,677,677,224,102,2,223,223,1006,224,614,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,629,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,644,1001,223,1,223,108,226,677,224,102,2,223,223,1005,224,659,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
|
||||
Reference in New Issue
Block a user