summaryrefslogtreecommitdiff
path: root/22_2024.py
blob: 1cff601f47053b5e54705ae9fc2efba5e20e1d3f (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
input = "data/22_2024.txt"
data = open(input).read().strip()


def mix(x, y):
    return x ^ y


def prune(x):
    return x % 16777216


def prices(x):
    res = [x]
    for _ in range(2000):
        x = prune(mix(x, 64 * x))
        x = prune(mix(x, x // 32))
        x = prune(mix(x, x * 2048))
        res.append(x)
    return res


def changes(p):
    return [p[i + 1] - p[i] for i in range(len(p) - 1)]


def getscores(p, c):
    res = {}
    for i in range(len(c) - 3):
        pattern = (c[i], c[i + 1], c[i + 2], c[i + 3])
        if pattern not in res:
            res[pattern] = p[i + 4]
    return res


p1, score = 0, {}
for line in data.split("\n"):
    p = prices(int(line))
    p1 += p[-1]
    p = [x % 10 for x in p]
    c = changes(p)
    s = getscores(p, c)
    for k, v in s.items():
        if k not in score:
            score[k] = v
        else:
            score[k] += v
print(p1)
print(max(score.values()))