blob: aa62086947d3978856fcd702425f405b11084c2c (
plain) (
blame)
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
|
#!/usr/bin/python3
import collections
from typing import Deque, Iterable
type Diff = tuple[int, int, int, int]
def main() -> None:
with open("input", "r") as f:
xs = map(int, f.readlines())
# START PART 1
print(sum(map(takelast, map(secrets, xs))))
# END PART 1 START PART 2
sums = collections.defaultdict(int)
for x in xs:
seen: set[Diff] = set()
dq: Deque[int] = collections.deque(maxlen=4)
gen = map(lastdigit, secrets(x))
last = next(gen)
for _ in range(3):
n = next(gen)
dq.append(n - last)
last = n
for n in gen:
dq.append(n - last)
if (t := tuple(dq)) not in seen:
seen.add(t)
sums[t] += n
last = n
dq.popleft()
print(max(sums.values()))
# END PART 2
def secrets(n: int) -> int:
for _ in range(2000):
n = (n ^ n<<6) & 0xFFFFFF
n = (n ^ n>>5) & 0xFFFFFF
n = (n ^ n<<11) & 0xFFFFFF
yield n
def lastdigit(n: int) -> int:
return n % 10
def takelast[T](xs: Iterable[T]) -> T:
*_, x = xs
return x
if __name__ == "__main__":
main()
|