diff --git a/tests/y_2024/test_2024_day10.py b/tests/y_2024/test_2024_day10.py index 63a9ee0..b95f618 100644 --- a/tests/y_2024/test_2024_day10.py +++ b/tests/y_2024/test_2024_day10.py @@ -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 ), ): @@ -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 diff --git a/y_2024/day10.py b/y_2024/day10.py index c3b0cef..143b420 100644 --- a/y_2024/day10.py +++ b/y_2024/day10.py @@ -20,28 +20,19 @@ 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())) @@ -49,14 +40,14 @@ def _calculate_1(self): == 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)) @@ -67,26 +58,19 @@ 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())) @@ -94,15 +78,13 @@ def _calculate_2(self): == 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 ( @@ -113,7 +95,6 @@ def _calculate_2(self): ): frontier.append(cur.ahead()) - # print(f"{len(visited)=}") result += visited - # break + return result