Skip to content

Commit

Permalink
day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Stegallo committed Dec 14, 2024
1 parent 12e241c commit 4b24339
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
24 changes: 10 additions & 14 deletions tests/y_2024/test_2024_day10.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
with patch(
"builtins.open",
mock_open(
read_data="""............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
read_data="""89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""", # noqa: E501
),
):
Expand All @@ -31,9 +27,9 @@ def test__preprocess_input():

def test_calculate_1():
r = day._calculate_1()
assert r == 14
assert r == 36


def test_calculate_2():
r = day._calculate_2()
assert r == 34
assert r == 81
37 changes: 9 additions & 28 deletions y_2024/day10.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,34 @@ def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
# self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data]
print(f"{self._input_data=}")
print(f"{len(self._input_data)=}")
print(f"{len(self._input_data[0])=}")
self.grid = Grid.from_input(self._input_data)
self.grid.display()
# self.__input_data = [Row(i) for i in self._input_data[0]]
self.__input_data = [Row(i) for j in self._input_data for i in j]

def _calculate_1(self):
result = 0
return 0
# print(self.grid.values['0'])

for i in self.grid.values["0"]:
# print(f"{i=}")
visited = set()
frontier = deque()
for j in DIRS:
cur = Cursor(i, Direction.from_symbol(j))

# print(f"{cur=}, {cur.ahead()=}, {self.grid.grid.get(cur.ahead())}")
# print(self.grid.grid.get(i))
if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(i))
== 1
):
frontier.append(cur.ahead())
# print(f"{frontier=}, {len(frontier)=}")

while len(frontier) > 0:
k = frontier.popleft()
# print(f"{k=}, {self.grid.grid.get(k)=}")

if self.grid.grid.get(k) == "9":
visited.add(k)
continue
# print(k, self.grid.grid.get(k))

for j in DIRS:
cur = Cursor(k, Direction.from_symbol(j))

Expand All @@ -67,42 +58,33 @@ def _calculate_1(self):
== 1
):
frontier.append(cur.ahead())
# if :
# visited.add()
# print(f"{len(visited)=}")

result += len(visited)
# break

# ...
return result

def _calculate_2(self):
result = 0
for i in self.grid.values["0"]:
# print(f"{i=}")
visited = 0
frontier = deque()
for j in DIRS:
cur = Cursor(i, Direction.from_symbol(j))

# print(f"{cur=}, {cur.ahead()=}, {self.grid.grid.get(cur.ahead())}")
# print(self.grid.grid.get(i))
if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(i))
== 1
):
frontier.append(cur.ahead())
print(f"{frontier=}, {len(frontier)=}")
# break

while len(frontier) > 0:
k = frontier.popleft()
# print(f"{k=}, {self.grid.grid.get(k)=}")

if self.grid.grid.get(k) == "9":
visited += 1
# continue
# print(k, self.grid.grid.get(k))

for j in DIRS:
cur = Cursor(k, Direction.from_symbol(j))
if (
Expand All @@ -113,7 +95,6 @@ def _calculate_2(self):
):
frontier.append(cur.ahead())

# print(f"{len(visited)=}")
result += visited
# break

return result

0 comments on commit 4b24339

Please sign in to comment.