solved 2024 day9 part 1
This commit is contained in:
parent
f232a551b1
commit
21421d265e
1
2024/day9/input.text
Normal file
1
2024/day9/input.text
Normal file
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
with open('test.text', 'r') as file:
|
||||
with open('input.text', 'r') as file:
|
||||
data: str = file.read().strip()
|
||||
|
||||
# If block has no ID then it is free
|
||||
@ -77,8 +77,8 @@ class DiskMap:
|
||||
|
||||
if remaining_free_size != 0:
|
||||
self.blocks[free_index] = FileBlock(free_size - remaining_free_size, file_id)
|
||||
self.blocks.insert(free_index + 1, FreeBlock(remaining_file_size))
|
||||
self.blocks[file_id] = FreeBlock(free_size)
|
||||
self.blocks.insert(free_index + 1, FreeBlock(remaining_free_size))
|
||||
self.blocks[file_index + 1] = FreeBlock(file_size)
|
||||
else:
|
||||
self.blocks[free_index] = FileBlock(free_size, file_id)
|
||||
self.blocks[file_index] = FreeBlock(free_size)
|
||||
@ -86,17 +86,26 @@ class DiskMap:
|
||||
if remaining_file_size:
|
||||
self.blocks.insert(file_index, FileBlock(remaining_file_size, file_id))
|
||||
|
||||
print(free_index)
|
||||
self.print()
|
||||
|
||||
def print(self) -> None:
|
||||
def __str__(self) -> str:
|
||||
s = ''
|
||||
for block in self.blocks:
|
||||
print(block, end='')
|
||||
print()
|
||||
s += str(block)
|
||||
return s
|
||||
|
||||
# Parse Data
|
||||
diskmap = DiskMap(data)
|
||||
|
||||
diskmap.print()
|
||||
# Part 1
|
||||
print(f'Input: {diskmap}')
|
||||
diskmap.compact()
|
||||
diskmap.print()
|
||||
print(f'Compacted: {diskmap}')
|
||||
|
||||
total = 0
|
||||
index = 0
|
||||
for block in diskmap.blocks:
|
||||
if type(block) == FileBlock:
|
||||
for i in range(block.size):
|
||||
total += block.id * index
|
||||
index += 1
|
||||
|
||||
print(f'Part 1: {total}')
|
||||
|
||||
@ -1 +1 @@
|
||||
12345
|
||||
2333133121414131402
|
||||
Loading…
x
Reference in New Issue
Block a user