-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.py
64 lines (56 loc) · 1.62 KB
/
day22.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
58
59
60
61
62
63
64
from collections import defaultdict, Counter, deque
from itertools import combinations, product, groupby, permutations
from re import findall
from heapq import *
from math import log10, ceil
from functools import cache
def lmap(f,it): return list(map(f, iter(it)))
def ints(it): return lmap(int, it)
def tmap(tf,it): return list(tuple(f(x) for f in iter(tf)) for x in iter(it))
def digits(n): return len(str(n)) # about 5% faster than doing a log10 approach for digits < 1e6
DELTAXY = [(1,0),(-1,0),(0,1),(0,-1)]
def gensecret(sec,c):
for _ in range(c):
n = sec*64
sec = sec ^ n
sec %= 16777216
n = sec // 32
sec = sec ^ n
sec %= 16777216
n = sec * 2048
sec = sec ^ n
sec %= 16777216
return sec
def part1(src):
s = 0
for n in src.splitlines():
s += gensecret(int(n), 2000)
return s
def gensecrets(sec,c):
arr = [(sec, sec % 10, None)]
for _ in range(c):
n = sec*64
sec = sec ^ n
sec %= 16777216
n = sec // 32
sec = sec ^ n
sec %= 16777216
n = sec * 2048
sec = sec ^ n
sec %= 16777216
p = sec % 10
arr.append((sec, p, p - arr[-1][1]))
return arr
def part2(src):
s = 0
secs = lmap(lambda n: gensecrets(int(n), 2000), src.splitlines())
chains = defaultdict(int)
for s in secs:
v = set()
for i in range(0, len(s) - 4):
p1,p2,p3,p4 = s[i:i+4]
chs = (p1[2],p2[2],p3[2],p4[2])
if chs not in v:
chains[chs] += p4[1]
v.add(chs)
return max(chains.values())