summaryrefslogtreecommitdiff
path: root/2024/22/python/main.py
blob: c2908582ab83e2d75ff4a937e83e5c096f0f1399 (plain)
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
from fileinput import input
from itertools import islice

lines = [int(line.strip()) for line in input()]


def secrets(secret):
    x = secret
    while True:
        yield x
        x ^= x << 6
        x &= 0xFFFFFF
        x ^= x >> 5
        x &= 0xFFFFFF
        x ^= x << 11
        x &= 0xFFFFFF


def prices(it):
    for s in it:
        yield s % 10


def diff(it):
    prev = next(it)
    for curr in it:
        yield curr, curr - prev
        prev = curr


def to_seqs(it, n=4):
    head = map(lambda x: x[1], islice(it, n - 1))
    q = deque(head, maxlen=n)
    for price, diff in it:
        q.append(diff)
        yield price, tuple(q)


def seq_sells(it):
    sells = {}
    for price, diff in it:
        if diff in sells:
            continue
        sells[diff] = price
    return sells


best_seqs = Counter()
for x in lines:
    best_seqs += seq_sells(to_seqs(islice(diff(prices(secrets(x))), 2000)))

silver = sum(next(islice(secrets(x), 2000, None)) for x in lines)
gold = best_seqs.most_common(1)[0][1]

print("silver:", silver)
print("gold:", gold)