Added a partially complete find_neighbours function

This commit is contained in:
Evie Litherland-Smith 2024-12-17 07:02:32 +00:00
parent 6c112f7be6
commit 16d8f293eb

View file

@ -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