From 16d8f293eb4b13fc5c4624324f685971c17d451c Mon Sep 17 00:00:00 2001 From: Evie Litherland-Smith Date: Tue, 17 Dec 2024 07:02:32 +0000 Subject: [PATCH] Added a partially complete find_neighbours function --- day4/run.py | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/day4/run.py b/day4/run.py index 1a5d35d..1413c32 100644 --- a/day4/run.py +++ b/day4/run.py @@ -34,21 +34,56 @@ class FindXMAS: def _to_grid(input: str) -> List[List[str]]: return [list(line) for line in input.splitlines() if line] + def _find_neighbours( + self, + origin: Tuple[int, int], + bounds: Tuple[Tuple[int, int], ...] = ((-1, 1), (-1, 1)), + locations: Tuple[Tuple[int, ...]] = (), + target: str = "XMAS", + ) -> Tuple[int, ...]: + if len(target) <= 1: + return locations + current = self.inputgrid[origin[0]][origin[1]] + next = target[target.find(current) + 1] + for row in range(origin[0] + bounds[0][0], origin[0] + bounds[0][1] + 1): + for col in range(origin[1] - bounds[1][0], origin[1] + bounds[1][1] + 1): + if any( + ( + row < 0, + row >= len(self.inputgrid[0]), + col < 0, + col >= len(self.inputgrid), + ) + ): + continue + if self.inputgrid[row][col].upper() == next.upper(): + return self._find_neighbours( + origin=(row, col), + bounds=( + (-1 if row < origin[0] else 0, 1 if row > origin[0] else 0), + (-1 if col < origin[1] else 0, 1 if col > origin[1] else 0), + ), + locations=( + locations, + (row, col), + ), + target=target[1:], + ) + def locate_xmas(self) -> List[Tuple[int, int, int, int]]: - locate: Tuple[int, ...] = [] + location: List[Tuple[int, ...]] = [] + for i, line in enumerate(self.inputgrid): for j, char in enumerate(line): if char.upper() == "X": - for k, contchar in enumerate(("M", "A", "S")): - offset = k + 1 - for row in range(i - offset, i + offset + 1): - for col in range(j - offset, j + offset + 1): - pass + location.append(self._find_neighbours((i, j), target="XMAS")) - return locate + return location if __name__ == "__main__": example = FindXMAS(EXAMPLE) - print(example) - assert example._part1 == 18 + print(example._find_neighbours((0, 4))) + print(example.locate_xmas()) + # print(example) + # assert example._part1 == 18