diff --git a/2016/d25/ex1/ex1.py b/2016/d25/ex1/ex1.py new file mode 100755 index 0000000..cd53244 --- /dev/null +++ b/2016/d25/ex1/ex1.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +import enum +import sys +from typing import NamedTuple + + +class Op(enum.StrEnum): + CPY = "cpy" + INC = "inc" + DEC = "dec" + JNZ = "jnz" + OUT = "out" + + +class Instruction(NamedTuple): + op: Op + x: str + y: str | None = None + + @classmethod + def from_str(cls, input: str) -> "Instruction": + op, *rest = input.split() + return cls(Op(op), *rest) + + +def solve(input: str) -> int: + def parse(input: str) -> list[Instruction]: + return [Instruction.from_str(line) for line in input.splitlines()] + + def next_signal_int(n: int) -> int: + res = 0b10 + while res < n: + res = (res << 2) | res + return res + + instructions = parse(input) + bc = int(instructions[1].x) * int(instructions[2].x) + return next_signal_int(bc) - bc + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()