Skip to content

Commit

Permalink
2024 day 4 - polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeni Gordeev committed Jan 6, 2025
1 parent 742d6db commit 14ba2bc
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions 2024/04.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ def read_input() -> str:


# MAIN
KEYWORD = 'XMAS'


def neighbors(x, y, h, w) -> List[Tuple[int, int]]:
return [(x + i, y + j) for i in (-1, 0, 1) for j in (-1, 0, 1)
if (i, j) != (0, 0) and -1 < x + i < h and -1 < y + j < w]


def part1(given: List[str]) -> int:
KEYWORD = 'XMAS'

def bfs(letters: List[str], paths: List[List[Tuple[int, int]]]):
if not paths:
return []
Expand All @@ -32,10 +31,10 @@ def bfs(letters: List[str], paths: List[List[Tuple[int, int]]]):
if letters[x][y] == KEYWORD[len(p) - 1]:
if len(p) == len(KEYWORD):
identified.append(p)
continue
for nx, ny in neighbors(x, y, len(letters), len(letters[0])):
if len(p) < 2 or p[-2][0] - x == x - nx and p[-2][1] - y == y - ny: # word must not break, i.e. have the same direction
identified.extend(bfs(letters, [p + [(nx, ny)]]))
else:
for nx, ny in neighbors(x, y, len(letters), len(letters[0])):
if len(p) < 2 or p[-2][0] - x == x - nx and p[-2][1] - y == y - ny: # word must not break, i.e. have the same direction
identified.extend(bfs(letters, [p + [(nx, ny)]]))

return identified

Expand Down

0 comments on commit 14ba2bc

Please sign in to comment.