-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_22.py
57 lines (43 loc) · 1.37 KB
/
day_22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from collections import Counter, deque
import aoc_helper
raw = aoc_helper.fetch(22, 2024)
def parse_raw(raw: str):
return [int(i) for i in raw.splitlines()]
data = parse_raw(raw)
class PRNG:
def __init__(self, seed:int):
self.seed = seed
self.mask = (1 << 24) - 1
def nex(self):
self.seed ^= ((self.seed << 6) & self.mask)
self.seed ^= ((self.seed >> 5) & self.mask)
self.seed ^= ((self.seed << 11) & self.mask)
return self.seed
def part_one(data=data):
sm = 0
for d in data:
c = PRNG(d)
for i in range(2000):
s = c.nex()
sm += s
return sm
aoc_helper.lazy_test(day=22, year=2024, parse=parse_raw, solution=part_one)
def part_two(data=data):
counter = Counter()
for d in data:
cur = set()
c = PRNG(d)
a = deque(maxlen=4)
last = d%10
for i in range(2000):
nx = c.nex() % 10
a.append((nx - last))
t = tuple(a)
if t not in cur and (len(t) == 4):
counter[t] += nx
cur.add(t)
last = nx
return counter.most_common(1)[0][1]
aoc_helper.lazy_test(day=22, year=2024, parse=parse_raw, solution=part_two)
aoc_helper.lazy_submit(day=22, year=2024, solution=part_one, data=data)
aoc_helper.lazy_submit(day=22, year=2024, solution=part_two, data=data)