val input = readText("y2019/w1/d02/input.txt") fun main() { val program = input.split(",").map(String::toInt).toMutableList() println("Part 1: " + run(program, 12, 2)) for (noun in 0 until 100) for (verb in 0 until 100) if (run(program, noun, verb) == 19690720) println("Part 2: " + (100 * noun + verb)) } private fun run(program: List, noun: Int, verb: Int): Int { val memory = program.toMutableList() memory[1] = noun memory[2] = verb var instrPtr = 0; loop@while (true) { val op: (Int, Int) -> Int = when(val opcode = memory[instrPtr]) { 1 -> Int::plus 2 -> Int::times 99 -> break@loop else -> error("unknown opcode $opcode") } memory[memory[instrPtr + 3]] = op(memory[memory[instrPtr + 1]], memory[memory[instrPtr + 2]]) instrPtr += 4 } return memory[0] }