61 lines
1.5 KiB
Python
Executable file
61 lines
1.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
# https://adventofcode.com/2024/day/9
|
|
|
|
from collections import deque
|
|
|
|
|
|
def get_lines(filename: str) -> list:
|
|
with open(filename, "r") as file:
|
|
return [line.strip() for line in file.readlines()]
|
|
|
|
|
|
def get_disk(disk_map: str) -> tuple:
|
|
disk = []
|
|
full_blocks = deque()
|
|
free_blocks = deque()
|
|
file_id = 0
|
|
head_pos = 0
|
|
for pos, count_str in enumerate(disk_map):
|
|
count = int(count_str)
|
|
if pos % 2 == 0:
|
|
for i in range(count):
|
|
disk.append(file_id)
|
|
full_blocks.append(head_pos)
|
|
head_pos += 1
|
|
file_id += 1
|
|
else:
|
|
for i in range(count):
|
|
disk.append(None)
|
|
free_blocks.append(head_pos)
|
|
head_pos += 1
|
|
return disk, free_blocks, full_blocks
|
|
|
|
|
|
def print_disk(disk: list) -> None:
|
|
for block in disk:
|
|
if block is None:
|
|
print(".", end="")
|
|
else:
|
|
print(block, end="")
|
|
print()
|
|
|
|
|
|
def defragment(disk: list, full_blocks: deque, free_blocks: deque) -> None:
|
|
while len(free_blocks) > 0:
|
|
pass
|
|
|
|
def main():
|
|
# disk_map = get_lines("sample-input.txt")[0]
|
|
disk_map = get_lines("sample-input2.txt")[0]
|
|
# disk_map = get_lines("input.txt")[0]
|
|
|
|
disk, free_blocks, full_blocks = get_disk(disk_map)
|
|
print_disk(disk)
|
|
# print(free_blocks)
|
|
# print(full_blocks)
|
|
defragment(disk, free_blocks, full_blocks)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|