-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.py
64 lines (46 loc) · 1.56 KB
/
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
58
59
60
61
62
63
64
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from collections import defaultdict, deque
from math import floor
input_value = open("22.txt", "r").read()
lines = input_value.split("\n")
secrets = [int(value) for value in lines]
def mix_with_secret(secret, value):
return secret ^ value
def prune_secret(secret):
return secret % 16777216
def get_next_secret(secret):
result_1 = secret * 64
secret = mix_with_secret(secret, result_1)
secret = prune_secret(secret)
result_2 = floor(secret / 32)
secret = mix_with_secret(secret, result_2)
secret = prune_secret(secret)
result_3 = secret * 2048
secret = mix_with_secret(secret, result_3)
secret = prune_secret(secret)
return secret
def get_next_secret_n(secret, n):
for _ in range(n):
secret = get_next_secret(secret)
return secret
# Part 1:
print(sum(get_next_secret_n(secret, 2000) for secret in secrets))
def populate_price_totals(delta_price_totals, secret, n):
deltas = deque()
seen_deltas = set()
for _ in range(n):
next_secret = get_next_secret(secret)
deltas.append(next_secret % 10 - secret % 10)
if len(deltas) == 4:
if tuple(deltas) not in seen_deltas:
seen_deltas.add(tuple(deltas))
delta_price_totals[tuple(deltas)] += next_secret % 10
deltas.popleft()
secret = next_secret
# Part 2:
delta_price_totals = defaultdict(int)
for secret in secrets:
populate_price_totals(delta_price_totals, secret, 2000)
print(max(delta_price_totals.values()))