From fbc63b138f0a33158408f7faea87d44428e3bdb7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 22 Dec 2024 00:50:22 -0500 Subject: [PATCH] 2024: d22: ex1: add solution --- 2024/d22/ex1/ex1.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 2024/d22/ex1/ex1.py diff --git a/2024/d22/ex1/ex1.py b/2024/d22/ex1/ex1.py new file mode 100755 index 0000000..78f06e0 --- /dev/null +++ b/2024/d22/ex1/ex1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: str) -> int: + def monkey_hash(seed: int) -> int: + MASK = (1 << 24) - 1 + seed ^= seed << 6 + seed &= MASK + + seed ^= seed >> 5 + seed &= MASK + + seed ^= seed << 11 + seed &= MASK + + return seed + + def run_rounds(seed: int) -> int: + for _ in range(2000): + seed = monkey_hash(seed) + return seed + + seeds = [int(n) for n in input.splitlines()] + return sum(map(run_rounds, seeds)) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()