Part 1 was relatively easy, 2 was harder. > So, by asking the monkey to sell the *first time* > each buyer's prices go down 2, then up 1, then > down 1, then up 3, you would get 23 (7 + 7 + 9) > bananas! Emphazis mine. Earlier versions of the code missed this fact, and added all occourences of the sequence. providing a answer that is too high.
44 lines
1 KiB
Python
44 lines
1 KiB
Python
import math
|
|
from collections import deque, defaultdict
|
|
|
|
from output import ints
|
|
|
|
|
|
def solve(data):
|
|
s = [(n, n % 10) for n in ints(data)]
|
|
bananas = defaultdict(int)
|
|
sum_2000th_secrets = 0
|
|
for j, n in enumerate(s):
|
|
seen = set()
|
|
L = deque()
|
|
for i in range(2000):
|
|
prize, dff = calc(*n)
|
|
if len(L) == 4:
|
|
L.popleft()
|
|
L.append(dff)
|
|
if len(L) == 4:
|
|
if tuple(L) not in seen:
|
|
seen.add(tuple(L))
|
|
bananas[tuple(L)] += prize % 10
|
|
n = prize, dff
|
|
sum_2000th_secrets += prize
|
|
max_bananas = max(bananas.values())
|
|
return sum_2000th_secrets, max_bananas
|
|
|
|
|
|
def calc(i, d=0):
|
|
a = i % 10
|
|
i = ((i * 64) ^ i) % 16777216
|
|
i = (math.floor(i / 32) ^ i) % 16777216
|
|
i = ((i * 2048) ^ i) % 16777216
|
|
return i, (i % 10) - a
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("./input/22.txt", "r") as f:
|
|
inp = f.read().strip()
|
|
|
|
p1, p2 = solve(inp)
|
|
|
|
print(p1)
|
|
print(p2)
|