This commit is contained in:
Evie Litherland-Smith 2024-12-22 15:47:46 +00:00
parent 2491ef0518
commit 0cc04808b3

View file

@ -27,8 +27,8 @@ class FindXMAS:
return len(self.locate_xmas()) return len(self.locate_xmas())
@property @property
def _part2(self) -> None: def _part2(self) -> int:
pass return len(self.locate_x_mas())
@staticmethod @staticmethod
def _to_grid(input: str) -> List[List[str]]: def _to_grid(input: str) -> List[List[str]]:
@ -58,7 +58,7 @@ class FindXMAS:
current.append((nx, ny)) current.append((nx, ny))
except IndexError: except IndexError:
pass pass
if len(current) == 4: if len(current) == len(target):
locations.append(tuple(current)) locations.append(tuple(current))
return locations return locations
@ -75,6 +75,31 @@ class FindXMAS:
return locations return locations
def locate_x_mas(self) -> List[Tuple[Tuple[int, ...]]]:
mas: List[Tuple[Tuple[int, ...]]] = []
for i, line in enumerate(self.inputgrid):
for j, char in enumerate(line):
if char.upper() == "M":
neighbours = [
val
for val in self.find_neighbours((i, j), target="MAS")
if all([len(set(part)) == len("MAS") for part in zip(*val)])
]
if neighbours is None:
continue
mas = [*mas, *neighbours]
locations: List[Tuple[Tuple[int, ...]]] = []
# breakpoint()
for loc in mas:
oth = [val[1] for val in mas if val != loc and val[::-1] != loc]
# breakpoint()
if loc[1] in oth:
locations.append(loc)
return mas
if __name__ == "__main__": if __name__ == "__main__":
example = FindXMAS(EXAMPLE) example = FindXMAS(EXAMPLE)