summaryrefslogtreecommitdiff
path: root/2024/09/python/main.py
blob: 8c8895d37ee88f60f91c2cd7d42b357081ac18ee (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
from fileinput import input

disk = list(input())[0].strip()

id_ = -1
blocks = []
tail = 0
file = True
for c in disk:
    blocks.append([tail, tail + int(c), (id_ := id_ + 1) if file else -1])
    tail += int(c)
    file = not file


expand_blocks = []
for a, b, x in sorted(blocks):
    for _ in range(b - a):
        expand_blocks.append(x)


def debug(xs):
    for a, b, x in sorted(xs):
        if x == -1:
            x = "."
        print(str(x) * (b - a), end="")
    print()


# Silver
tail = len(expand_blocks) - 1
fill = expand_blocks.index(-1)
while fill < tail:
    expand_blocks[fill] = expand_blocks[tail]
    expand_blocks[tail] = -1
    while expand_blocks[tail] == -1:
        tail -= 1
    while expand_blocks[fill] != -1:
        fill += 1

silver = sum(idx * n for idx, n in enumerate(expand_blocks) if n != -1)

print(blocks)

gold = 0

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