Continue with 2016 and 2017.

This commit is contained in:
2024-05-04 09:25:24 -04:00
parent 6e0cd511cc
commit 29ab4f3496
6 changed files with 479 additions and 1 deletions

57
2016/d14.py Normal file
View File

@@ -0,0 +1,57 @@
import hashlib
from dataclasses import dataclass
@dataclass
class Candidate:
char: str
age: int
orig_hash: int
conf_hash: int = 0
data = "cuanljph"
# data = "abc"
keys = []
candidates = []
rehashs = 2016 # part 2
rehashs = 0 # part 1
for hash_idx in range(0, 100_000):
h = hashlib.md5(f"{data}{hash_idx}".encode()).hexdigest()
for _ in range(rehashs):
h = hashlib.md5(h.encode()).hexdigest()
idx3 = None
for i in range(len(h) - 3):
if all([h[i] == h[i + j] for j in range(3)]):
idx3 = i
break
if idx3 is not None:
candidates.append(Candidate(h[idx3], 0, hash_idx))
letters = []
for i in range(len(h) - 5):
if all([h[i] == h[i + j] for j in range(5)]):
letters.append(h[i])
for current_char in set(letters):
for c in candidates:
if c.char == current_char and c.age > 0:
# print(f"Confirmed {c} at {hash_idx}.")
keys.append(c)
c.age = 1000 # mark for removal from candidates
c.conf_hash = hash_idx
if len(keys) >= 64:
# print("done")
break
# remove old candidates
candidates = [c for c in candidates if c.age < 1000]
for c in candidates:
c.age += 1
keys.sort(key=lambda c: c.orig_hash)
print(keys[63].orig_hash)

45
2016/d15.py Normal file
View File

@@ -0,0 +1,45 @@
import sys
from lib import str_to_ints
from copy import deepcopy
with open("i15.txt") as f:
d = f.read()
part_2 = True
if part_2:
d += "#7 11 0 0"
discs = []
for line in d.splitlines():
id, npos, time, cpos = str_to_ints(line)
discs.append([cpos, npos])
qpos = -1
orig_discs = deepcopy(discs)
# empirically could dev algo to find or CRT?
if part_2:
start, delta = 12136, 33915
else:
start, delta = 831, 2261
for start_time in range(start, 1_000_000_000, delta):
discs = deepcopy(orig_discs)
# let discs move for initial wait
for _ in range(start_time):
for disc in discs:
disc[0] = (disc[0] + 1) % disc[1]
# drop the capsule
for i in range(len(discs)):
# discs move first
for disc in discs:
disc[0] = (disc[0] + 1) % disc[1]
# check if there is a collission for the current disc
if discs[i][0] != 0:
break
else:
print(start_time)
sys.exit(0)