56 lines
961 B
Python
56 lines
961 B
Python
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
input_file = sys.argv[1]
|
|
|
|
with open(input_file) as fd:
|
|
lines = [line.rstrip() for line in fd.readlines()]
|
|
|
|
line = lines[0]
|
|
|
|
disk: list[int | None] = list()
|
|
|
|
fileno = 0
|
|
isfile = True
|
|
for char in line:
|
|
n = int(char)
|
|
if isfile:
|
|
disk += [fileno] * n
|
|
fileno += 1
|
|
else:
|
|
disk += [None] * n
|
|
isfile = not isfile
|
|
|
|
beg = 0
|
|
end = len(disk) - 1
|
|
|
|
|
|
def print_disk() -> None:
|
|
return
|
|
print("".join(str(c) if c is not None else "." for c in disk))
|
|
|
|
|
|
print_disk()
|
|
moving = False
|
|
while beg < end:
|
|
if moving:
|
|
if disk[end] is not None:
|
|
disk[beg] = disk[end]
|
|
disk[end] = None
|
|
moving = False
|
|
print_disk()
|
|
end -= 1
|
|
else:
|
|
if disk[beg] is None:
|
|
moving = True
|
|
else:
|
|
beg += 1
|
|
|
|
checksum = 0
|
|
for c, cid in enumerate(disk):
|
|
if cid is not None:
|
|
checksum += c * cid
|
|
|
|
print(checksum)
|