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 b6f38c2 commit 228b71d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
Binary file modified 2024/04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified 2024/04.pstats
Binary file not shown.
57 changes: 30 additions & 27 deletions 2024/04.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,50 @@ def read_input() -> str:


# MAIN
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(letters: List[str]) -> int:
keyword = 'XMAS'
height, width = len(letters), len(letters[0])

def part1(given: List[str]) -> int:
KEYWORD = 'XMAS'
def neighbors(x, y) -> 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 < height and -1 < y + j < width]

def bfs(letters: List[str], paths: List[List[Tuple[int, int]]]):
if not paths:
return []
identified = []
def bfs(paths: List[List[Tuple[int, int]]]):
# identified = []
counter = 0
for p in paths:
x, y = p[-1]
if letters[x][y] == KEYWORD[len(p) - 1]:
if len(p) == len(KEYWORD):
identified.append(p)
if letters[x][y] == keyword[len(p) - 1]:
if len(p) == len(keyword):
# identified.append(p)
counter += 1
else:
for nx, ny in neighbors(x, y, len(letters), len(letters[0])):
for nx, ny in neighbors(x, y):
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)]]))
# identified.extend(bfs([p + [(nx, ny)]]))
counter += bfs([p + [(nx, ny)]])

return identified
# return identified
return counter

height, width = len(given), len(given[0])
found = []
# found = []
result = 0
for x in range(height):
for y in range(width):
f = bfs(given, [[(x, y)]])
found.extend(f)
return len(found)
# found.extend(bfs([[(x, y)]]))
result += bfs([[(x, y)]])
# return len(found)
return result


def part2(given: List[str]) -> int:
def get_x_chars(letters, x, y):
return ''.join(letters[i][j] for i, j in [(x - 1, y - 1), (x - 1, y + 1), (x + 1, y - 1), (x + 1, y + 1)])

height, width = len(given), len(given[0])
found = []
# found = []
result = 0
for x, line in enumerate(given):
for y, char in enumerate(line):
if (
Expand All @@ -63,16 +68,14 @@ def get_x_chars(letters, x, y):
# ..A.. OR ..A.. OR ..A.. OR ..A..
# .S.S. .M.S. .M.M. .S.M.
):
found.append((x, y))
return len(found)
# found.append((x, y))
result += 1
# return len(found)
return result


# TEST
def test():
assert neighbors(0, 0, 2, 2) == [(0, 1), (1, 0), (1, 1)]
assert neighbors(1, 1, 2, 2) == [(0, 0), (0, 1), (1, 0)]
assert neighbors(1, 1, 3, 3) == [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)]

given = parser("""
MMMSXXMASM
MSAMXMSMSA
Expand Down
4 changes: 2 additions & 2 deletions 2024/benchmark-m1.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Benchmark 1: find 2024 -type f -regex ".*/[0-9]*\.py" | sort -n | xargs -L 1 python
Time (mean ± σ): 133.3 ms ± 8.5 ms [User: 115.9 ms, System: 15.2 ms]
Range (min … max): 128.4 ms … 156.5 ms 10 runs
Time (mean ± σ): 134.9 ms ± 6.3 ms [User: 114.9 ms, System: 16.5 ms]
Range (min … max): 127.5 ms … 147.1 ms 10 runs

0 comments on commit 228b71d

Please sign in to comment.