From cc916af0b7d81bc6c03d24dcf71d6ffcae220718 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 11 May 2025 23:14:23 +0100 Subject: [PATCH] 2017: d23: ex2: add solution --- 2017/d23/ex2/ex2.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 2017/d23/ex2/ex2.py diff --git a/2017/d23/ex2/ex2.py b/2017/d23/ex2/ex2.py new file mode 100755 index 0000000..cf18a89 --- /dev/null +++ b/2017/d23/ex2/ex2.py @@ -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()