Skip to content

Commit 90e7bdb

Browse files
committed
Add solution to 2024-12-22
1 parent 51f1abf commit 90e7bdb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2024/day22/solutions.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import defaultdict
2+
import numpy as np
3+
4+
with open("input") as f:
5+
ns = list(map(int, f.read().strip().split("\n")))
6+
7+
8+
def hsh(secret):
9+
for _ in range(2000):
10+
secret ^= secret << 6 & 0xFFFFFF
11+
secret ^= secret >> 5 & 0xFFFFFF
12+
secret ^= secret << 11 & 0xFFFFFF
13+
yield secret
14+
15+
16+
secrets = list(map(list, map(hsh, ns)))
17+
18+
# Part 1
19+
print(sum(s[-1] for s in secrets))
20+
21+
# Part 2
22+
result = defaultdict(int)
23+
for n in ns:
24+
ss = [s % 10 for s in hsh(n)]
25+
diffs = np.diff(ss)
26+
changes = set()
27+
for i in range(1996):
28+
if (change := tuple(diffs[i : i + 4])) not in changes:
29+
changes.add(change)
30+
result[change] += ss[i + 4]
31+
32+
print(max(result.values()))

0 commit comments

Comments
 (0)