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/solve.py | |
parent | 0a5c1f77d01fc0bd166494787f24562e2fd3a9e9 (diff) |
2024 day9-10
Diffstat (limited to '2024/day09/solve.py')
-rw-r--r-- | 2024/day09/solve.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/2024/day09/solve.py b/2024/day09/solve.py new file mode 100644 index 0000000..1b0f759 --- /dev/null +++ b/2024/day09/solve.py @@ -0,0 +1,35 @@ +with open("input") as f:
+ data = f.read()
+
+blocks = []
+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)
+
+empty_seek_idx = 0
+last_non_empty_idx = len(blocks) - 1
+while empty_seek_idx < last_non_empty_idx:
+ if not blocks[empty_seek_idx] == ".":
+ empty_seek_idx += 1
+ continue
+
+ if blocks[last_non_empty_idx] == ".":
+ last_non_empty_idx -= 1
+ continue
+
+ blocks[empty_seek_idx], blocks[last_non_empty_idx] = blocks[last_non_empty_idx], blocks[empty_seek_idx]
+
+
+def calculate_checksum(blocks: list[str]):
+ checksum = 0
+ for index, elem in enumerate(blocks):
+ if elem == ".":
+ break
+
+ checksum += index * int(elem)
+
+ return checksum
+
+
+print(calculate_checksum(blocks))
|