2016: d14: ex1: add solution

This commit is contained in:
Bruno BELANYI 2025-05-19 20:26:02 +01:00
parent 2aad8aa38a
commit 87ce54f838

42
2016/d14/ex1/ex1.py Executable file
View file

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