From 52a891aa57453759b96f9bd87077560f026e9d78 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 30 Dec 2024 20:17:54 -0500 Subject: [PATCH] 2018: d19: ex1: add solution --- 2018/d19/ex1/ex1.py | 98 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 2018/d19/ex1/ex1.py diff --git a/2018/d19/ex1/ex1.py b/2018/d19/ex1/ex1.py new file mode 100755 index 0000000..09c4a80 --- /dev/null +++ b/2018/d19/ex1/ex1.py @@ -0,0 +1,98 @@ +#!/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()