From 87ce54f8380b32845bc4c7c4260b9e4f34e84dde Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 19 May 2025 20:26:02 +0100 Subject: [PATCH] 2016: d14: ex1: add solution --- 2016/d14/ex1/ex1.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 2016/d14/ex1/ex1.py diff --git a/2016/d14/ex1/ex1.py b/2016/d14/ex1/ex1.py new file mode 100755 index 0000000..b960e4b --- /dev/null +++ b/2016/d14/ex1/ex1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import functools +import hashlib +import itertools +import sys + + +def solve(input: str) -> int: + def hash(n: int, salt: str) -> str: + return hashlib.md5((salt + str(n)).encode()).hexdigest() + + def find_triplet_char(input: str) -> str | None: + for i in range(0, len(input) - 2): + a, b, c = input[i], input[i + 1], input[i + 2] + if a == b and b == c: + return a + return None + + salt = input.strip() + hashed_salt = functools.cache(functools.partial(hash, salt=salt)) + cur_key = 0 + for i in itertools.count(): + candidate = hashed_salt(i) + if (triplet_char := find_triplet_char(candidate)) is None: + continue + for j in range(i + 1, i + 1000 + 1): + if (triplet_char * 5) in hashed_salt(j): + cur_key += 1 + if cur_key == 64: + return i + break + assert False # Sanity check + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()