99 lines
2.7 KiB
Python
Executable file
99 lines
2.7 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
import copy
|
|
import enum
|
|
import sys
|
|
from typing import NamedTuple
|
|
|
|
|
|
class OpCode(enum.StrEnum):
|
|
ADDR = "addr"
|
|
ADDI = "addi"
|
|
MULR = "mulr"
|
|
MULI = "muli"
|
|
BANR = "banr"
|
|
BANI = "bani"
|
|
BORR = "borr"
|
|
BORI = "bori"
|
|
SETR = "setr"
|
|
SETI = "seti"
|
|
GTIR = "gtir"
|
|
GTRI = "gtri"
|
|
GTRR = "gtrr"
|
|
EQIR = "eqir"
|
|
EQRI = "eqri"
|
|
EQRR = "eqrr"
|
|
|
|
def apply(self, registers: list[int], a: int, b: int, c: int) -> list[int]:
|
|
registers = copy.deepcopy(registers)
|
|
if self == OpCode.ADDR:
|
|
registers[c] = registers[a] + registers[b]
|
|
if self == OpCode.ADDI:
|
|
registers[c] = registers[a] + b
|
|
if self == OpCode.MULR:
|
|
registers[c] = registers[a] * registers[b]
|
|
if self == OpCode.MULI:
|
|
registers[c] = registers[a] * b
|
|
if self == OpCode.BANR:
|
|
registers[c] = registers[a] & registers[b]
|
|
if self == OpCode.BANI:
|
|
registers[c] = registers[a] & b
|
|
if self == OpCode.BORR:
|
|
registers[c] = registers[a] | registers[b]
|
|
if self == OpCode.BORI:
|
|
registers[c] = registers[a] | b
|
|
if self == OpCode.SETR:
|
|
registers[c] = registers[a]
|
|
if self == OpCode.SETI:
|
|
registers[c] = a
|
|
if self == OpCode.GTIR:
|
|
registers[c] = a > registers[b]
|
|
if self == OpCode.GTRI:
|
|
registers[c] = registers[a] > b
|
|
if self == OpCode.GTRR:
|
|
registers[c] = registers[a] > registers[b]
|
|
if self == OpCode.EQIR:
|
|
registers[c] = a == registers[b]
|
|
if self == OpCode.EQRI:
|
|
registers[c] = registers[a] == b
|
|
if self == OpCode.EQRR:
|
|
registers[c] = registers[a] == registers[b]
|
|
return registers
|
|
|
|
|
|
class Instruction(NamedTuple):
|
|
op: OpCode
|
|
a: int
|
|
b: int
|
|
c: int
|
|
|
|
def apply(self, registers: list[int]) -> list[int]:
|
|
return self.op.apply(registers, self.a, self.b, self.c)
|
|
|
|
|
|
def solve(input: str) -> int:
|
|
def parse_instruction(input: str) -> Instruction:
|
|
op, *values = input.split()
|
|
return Instruction(OpCode(op), *map(int, values))
|
|
|
|
def parse(input: list[str]) -> tuple[int, list[Instruction]]:
|
|
ip = int(input[0].removeprefix("#ip "))
|
|
return ip, [parse_instruction(line) for line in input[1:]]
|
|
|
|
ip_reg, instructions = parse(input.splitlines())
|
|
registers = [0] * 6
|
|
|
|
while (ip := registers[ip_reg]) < len(instructions):
|
|
registers = instructions[ip].apply(registers)
|
|
registers[ip_reg] += 1
|
|
return registers[0]
|
|
|
|
|
|
def main() -> None:
|
|
input = sys.stdin.read()
|
|
print(solve(input))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|