Skip to content

Commit 49f8bd2

Browse files
committed
day 9 part 1
1 parent e778834 commit 49f8bd2

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Day 9 (2024)
2+
3+
`TITLE` ([prompt](https://adventofcode.com/2024/day/9))
4+
5+
Use this space for notes on the day's solution and to document what you've learned!
6+
7+
## Part 1
8+
9+
## Part 2
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2333133121414131402
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Generated using @xavdid's AoC Python Template: https://github.com/xavdid/advent-of-code-python-template
2+
3+
# puzzle prompt: https://adventofcode.com/2024/day/9
4+
5+
from ...base import TextSolution, answer
6+
7+
8+
class Solution(TextSolution):
9+
_year = 2024
10+
_day = 9
11+
12+
# @answer(1234)
13+
def part_1(self) -> int:
14+
compacted = list(self.input)
15+
self.disk = []
16+
17+
for index, char in enumerate(compacted):
18+
block_len = int(char)
19+
20+
if index % 2 == 0:
21+
for _i in range(block_len):
22+
self.disk.append(index // 2)
23+
else:
24+
for _i in range(block_len):
25+
self.disk.append(None)
26+
27+
left_pointer = 0
28+
right_pointer = (
29+
len(self.disk) - 1 if self.disk[-1] is not None else len(self.disk) - 2
30+
)
31+
32+
while left_pointer < right_pointer:
33+
if self.disk[left_pointer] is None and self.disk[right_pointer] is not None:
34+
self._move_block(left_pointer, right_pointer)
35+
right_pointer -= 1
36+
left_pointer += 1
37+
elif self.disk[left_pointer] is not None:
38+
left_pointer += 1
39+
else:
40+
right_pointer -= 1
41+
42+
total = 0
43+
for idx, num in enumerate(self.disk):
44+
if num is None:
45+
break
46+
total += idx * num
47+
return total
48+
49+
def _move_block(self, left_index, right_index):
50+
self.disk[left_index] = self.disk[right_index]
51+
self.disk[right_index] = None
52+
53+
# @answer(1234)
54+
def part_2(self) -> int:
55+
pass
56+
57+
# @answer((1234, 4567))
58+
# def solve(self) -> tuple[int, int]:
59+
# pass

0 commit comments

Comments
 (0)