aboutsummaryrefslogtreecommitdiff
path: root/2024/22/solve.py
blob: f6fe00f9ad1c70412a0e5b4103861ac4d4b041f6 (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
#!/bin/env python3

L = [int(l.strip()) for l in open("input").readlines()]


def evolve(n):
    n ^= n * 64
    n %= 16777216

    n ^= n // 32
    n %= 16777216

    n ^= n * 2048
    n %= 16777216

    return n


def part1(n):
    for i in range(2000):
        n = evolve(n)
    return n


def part2():
    M = {}
    for n in L:
        diffs = []
        vals = [0]
        for i in range(2000):
            n = evolve(n)
            s = n % 10
            diffs.append(s - vals[-1])
            vals.append(s)

        s = set()
        for i in range(4, len(vals)):
            r = tuple(diffs[i - 4 : i])
            if r not in s:
                if r in M:
                    M[r] += vals[i]
                else:
                    M[r] = vals[i]
                s.add(r)
    return M[max(M, key=M.get)]


print(sum(part1(n) for n in L))
print(part2())