summaryrefslogtreecommitdiff
path: root/9_2024.py
blob: 16a4c2fe0f4342d5dfa43c023a7a28d5949c92f6 (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
from collections import deque

input = "data/9_2024.txt"
data = open(input).read().strip()


def solve(part2):
    q, space = deque([]), deque([])
    id, pos, final = 0, 0, []
    for i, c in enumerate(data):
        if i % 2 == 0:
            if part2:
                q.append((pos, int(c), id))
            for i in range(int(c)):
                final.append(id)
                if not part2:
                    q.append((pos, 1, id))
                pos += 1
            id += 1
        else:
            space.append((pos, int(c)))
            for i in range(int(c)):
                final.append(None)
                pos += 1
    for pos, size, id in reversed(q):
        for space_i, (space_pos, space_size) in enumerate(space):
            if space_pos < pos and size <= space_size:
                for i in range(size):
                    assert final[pos + i] == id, f"{final[pos+i]=}"
                    final[pos + i] = None
                    final[space_pos + i] = id
                space[space_i] = (space_pos + size, space_size - size)
                break
    res = 0
    for i, c in enumerate(final):
        if c is not None:
            res += i * c
    return res


p1 = solve(False)
p2 = solve(True)

print(p1)
print(p2)