40 lines
828 B
Python
40 lines
828 B
Python
import time
|
|
start_time = time.time()
|
|
|
|
with open('puzzle.txt', 'r') as f:
|
|
line = f.read()
|
|
|
|
disk_string = []
|
|
|
|
file = True
|
|
|
|
file_id = 0
|
|
|
|
for char in list(line):
|
|
if file:
|
|
disk_string += [str(file_id)] * int(char)
|
|
file_id += 1
|
|
|
|
else:
|
|
disk_string += ['.'] * int(char)
|
|
|
|
file = not file
|
|
|
|
for index_a, char_a in enumerate(disk_string[::-1]):
|
|
if char_a != '.':
|
|
for index_b, char_b in enumerate(disk_string):
|
|
if char_b == '.':
|
|
disk_string[index_b] = char_a
|
|
disk_string.pop(-1)
|
|
while disk_string[-1] == '.':
|
|
disk_string.pop(-1)
|
|
break
|
|
|
|
checksum = 0
|
|
|
|
for index, char in enumerate(disk_string):
|
|
checksum += int(char) * index
|
|
|
|
print(checksum)
|
|
print(f'Total time: {time.time() - start_time}')
|