2017: d23: ex2: add solution

This commit is contained in:
Bruno BELANYI 2025-05-11 23:14:23 +01:00
parent 3e51e4176f
commit cc916af0b7

55
2017/d23/ex2/ex2.py Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
import enum
import math
import sys
from typing import NamedTuple
class Op(enum.StrEnum):
SET = "set"
SUB = "sub"
MUL = "mul"
JNZ = "jnz"
class Instruction(NamedTuple):
op: Op
x: str
y: str
@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 is_prime(n: int) -> bool:
if n % 2 == 0:
return False
for i in range(3, math.isqrt(n), 2):
if n % i == 0:
return False
return True
instructions = parse(input)
start = int(instructions[0].y) * 100 + 100000
end = start + 17000
total = 0
for n in range(start, end + 1, 17):
total += not is_prime(n)
return total
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()