diff options
author | Botond Hende <nettingman@gmail.com> | 2024-12-11 10:20:30 +0100 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-12-11 10:20:30 +0100 |
commit | 3b1b3fe99e6953b53a44c070d227ad36341e71c4 (patch) | |
tree | dc6d94cbbe54997568777a208d8e15c074d38529 /2024/day09/solve2.py | |
parent | 0a5c1f77d01fc0bd166494787f24562e2fd3a9e9 (diff) |
2024 day9-10
Diffstat (limited to '2024/day09/solve2.py')
-rw-r--r-- | 2024/day09/solve2.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/2024/day09/solve2.py b/2024/day09/solve2.py new file mode 100644 index 0000000..bed2825 --- /dev/null +++ b/2024/day09/solve2.py @@ -0,0 +1,58 @@ +with open("input") as f:
+ data = f.read()
+
+blocks = []
+last_file_id = None
+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)
+ last_file_id = block_char
+
+end_seek_idx = len(blocks) - 1
+for file in range(int(last_file_id), -1, -1):
+ file_block = str(file)
+ file_start_block = None
+ file_end_block = None
+ for idx in range(end_seek_idx, -1, -1):
+ if blocks[idx] == file_block:
+ if file_end_block is None:
+ file_end_block = idx
+ else:
+ if file_end_block is not None:
+ file_start_block = idx + 1
+ end_seek_idx = idx
+ break
+ else:
+ break
+
+ file_size = file_end_block - file_start_block + 1
+
+ empty_block_size = 0
+ for idx, block_content in enumerate(blocks):
+ if idx > end_seek_idx:
+ break
+
+ if block_content == ".":
+ empty_block_size += 1
+ else:
+ empty_block_size = 0
+
+ if empty_block_size == file_size:
+ empty_block_start = idx - file_size + 1
+ for ii in range(file_size):
+ blocks[empty_block_start + ii], blocks[file_start_block + ii] = blocks[file_start_block + ii], blocks[empty_block_start + ii]
+ break
+
+def calculate_checksum(blocks: list[str]):
+ checksum = 0
+ for index, elem in enumerate(blocks):
+ if elem == ".":
+ continue
+
+ checksum += index * int(elem)
+
+ return checksum
+
+
+print(calculate_checksum(blocks))
|