diff --git a/asembunny.py b/asembunny.py index 2f956af..019d5cc 100644 --- a/asembunny.py +++ b/asembunny.py @@ -7,20 +7,32 @@ class Command(str, Enum): DECREASE = "dec" JUMP_NON_ZERO = "jnz" TOGGLE = "tgl" + OUTPUT = "out" class Computer: def __init__( self, code: list[tuple[Command, str | int, ...]], reg_a: int = 0, reg_b: int = 0, reg_c: int = 0, reg_d: int = 0 ): + self.code = code + self.max_output_len = int(1e12) + self.reg = {} + self.instr_ptr = 0 + self.output = [] + self.reset(reg_a=reg_a, reg_b=reg_b, reg_c=reg_c, reg_d=reg_d) + + def reset(self, reg_a: int = 0, reg_b: int = 0, reg_c: int = 0, reg_d: int = 0): self.reg = { "a": reg_a, "b": reg_b, "c": reg_c, "d": reg_d, } - self.code = code self.instr_ptr = 0 + self.output = [] + + def set_max_output_len(self, count: int) -> None: + self.max_output_len = count def get_value(self, source: int | str) -> int: if isinstance(source, int): @@ -46,6 +58,13 @@ class Computer: elif cmd == Command.DECREASE: self.reg[args[0]] -= 1 self.instr_ptr += 1 + elif cmd == Command.OUTPUT: + self.output.append(self.get_value(args[0])) + if len(self.output) >= self.max_output_len or ( + len(self.output) > 1 and self.output[-1] == self.output[-2] + ): + break + self.instr_ptr += 1 elif cmd == Command.JUMP_NON_ZERO: if isinstance(args[0], int): check = args[0] diff --git a/day25.py b/day25.py new file mode 100644 index 0000000..87a713f --- /dev/null +++ b/day25.py @@ -0,0 +1,33 @@ +from asembunny import Computer, parse_assembunny_code +from tools.aoc import AOCDay +from typing import Any + + +class Day(AOCDay): + inputs = [ + [ + (198, "input25"), + ], + [ + (None, "input25"), + ], + ] + + def part1(self) -> Any: + computer = Computer(parse_assembunny_code(self.getInput())) + computer.set_max_output_len(20) + ans = 0 + while True: + computer.reset(reg_a=ans) + computer.run() + if computer.output == [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]: + return ans + ans += 1 + + def part2(self) -> Any: + return "" + + +if __name__ == "__main__": + day = Day(2016, 25) + day.run(verbose=True) diff --git a/inputs/input25 b/inputs/input25 new file mode 100644 index 0000000..30cc487 --- /dev/null +++ b/inputs/input25 @@ -0,0 +1,30 @@ +cpy a d +cpy 4 c +cpy 633 b +inc d +dec b +jnz b -2 +dec c +jnz c -5 +cpy d a +jnz 0 0 +cpy a b +cpy 0 a +cpy 2 c +jnz b 2 +jnz 1 6 +dec b +dec c +jnz c -4 +inc a +jnz 1 -7 +cpy 2 b +jnz c 2 +jnz 1 4 +dec b +dec c +jnz 1 -4 +jnz 0 0 +out b +jnz a -19 +jnz 1 -21