2016: d25: ex1: add solution

This commit is contained in:
Bruno BELANYI 2025-05-20 02:01:35 +01:00
parent 013f583944
commit 7b161434a5

48
2016/d25/ex1/ex1.py Executable file
View file

@ -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()