blob: 1b0f75940abc396aa09ca7a129ec981dd852dca8 (
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
|
with open("input") as f:
data = f.read()
blocks = []
for idx, count in enumerate(data):
block_char = "." if idx % 2 == 1 else str(idx // 2)
for ii in range(int(count)):
blocks.append(block_char)
empty_seek_idx = 0
last_non_empty_idx = len(blocks) - 1
while empty_seek_idx < last_non_empty_idx:
if not blocks[empty_seek_idx] == ".":
empty_seek_idx += 1
continue
if blocks[last_non_empty_idx] == ".":
last_non_empty_idx -= 1
continue
blocks[empty_seek_idx], blocks[last_non_empty_idx] = blocks[last_non_empty_idx], blocks[empty_seek_idx]
def calculate_checksum(blocks: list[str]):
checksum = 0
for index, elem in enumerate(blocks):
if elem == ".":
break
checksum += index * int(elem)
return checksum
print(calculate_checksum(blocks))
|