aboutsummaryrefslogtreecommitdiff
path: root/2023/21/solve.py
blob: 8c87408ee430d22d4f0aaa42122d1c5f096bd14a (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
L = {
    (i, j): c
    for (i, l) in enumerate(open("input").readlines())
    for (j, c) in enumerate(l.strip())
}

W = max(j for (i, j) in L.keys()) + 1
C = W // 2


# from day 9
def predict(s):
    f = []
    while s:
        f.append(s[-1])
        s = [s[i] - s[i - 1] for i in range(1, len(s))]
    return sum(f)


# more efficient than predict
from numpy.polynomial import Polynomial


FIN = 26501365
PREHEAT = 5
LIM1 = 64
LIM2 = C + PREHEAT * W

pre = {-1: 0}
s = [(i, j) for (i, j) in L if L[(i, j)] == "S"][0]
q = [(s, 0)]
v = set()

part1 = 0

while q:
    p, n = q.pop(0)
    if n == LIM1 and not part1:
        part1 = pre[LIM1 - 1]
    if n == LIM2:
        break

    for move in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
        np = (p[0] + move[0], p[1] + move[1])
        _np = (np[0] % W, np[1] % W)
        if _np not in L or np in v or L[_np] == "#":
            continue
        q.append((np, n + 1))
        v.add(np)
        if n % 2:
            if n not in pre:
                pre[n] = pre[n - 2]
            pre[n] += 1

print(part1)

poly = {}
for i, p in pre.items():
    if (i + 1) % W == C:
        poly[i + 1] = p

f = Polynomial.fit(list(poly.keys()), list(poly.values()), 2)
res = f(FIN)
print(f, round(res))

# for i in range(PREHEAT, (FIN - C) / W + 1):
#     poly.append(predict(poly))