Skip to content

Commit 228b71d

Browse files
author
Evgeni Gordeev
committed
2024 day 4 - polish
1 parent b6f38c2 commit 228b71d

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

2024/04.png

364 Bytes
Loading

2024/04.pstats

6.05 KB
Binary file not shown.

2024/04.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,50 @@ def read_input() -> str:
1414

1515

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

18+
def part1(letters: List[str]) -> int:
19+
keyword = 'XMAS'
20+
height, width = len(letters), len(letters[0])
2121

22-
def part1(given: List[str]) -> int:
23-
KEYWORD = 'XMAS'
22+
def neighbors(x, y) -> List[Tuple[int, int]]:
23+
return [(x + i, y + j) for i in (-1, 0, 1) for j in (-1, 0, 1)
24+
if (i, j) != (0, 0) and -1 < x + i < height and -1 < y + j < width]
2425

25-
def bfs(letters: List[str], paths: List[List[Tuple[int, int]]]):
26-
if not paths:
27-
return []
28-
identified = []
26+
def bfs(paths: List[List[Tuple[int, int]]]):
27+
# identified = []
28+
counter = 0
2929
for p in paths:
3030
x, y = p[-1]
31-
if letters[x][y] == KEYWORD[len(p) - 1]:
32-
if len(p) == len(KEYWORD):
33-
identified.append(p)
31+
if letters[x][y] == keyword[len(p) - 1]:
32+
if len(p) == len(keyword):
33+
# identified.append(p)
34+
counter += 1
3435
else:
35-
for nx, ny in neighbors(x, y, len(letters), len(letters[0])):
36+
for nx, ny in neighbors(x, y):
3637
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
37-
identified.extend(bfs(letters, [p + [(nx, ny)]]))
38+
# identified.extend(bfs([p + [(nx, ny)]]))
39+
counter += bfs([p + [(nx, ny)]])
3840

39-
return identified
41+
# return identified
42+
return counter
4043

41-
height, width = len(given), len(given[0])
42-
found = []
44+
# found = []
45+
result = 0
4346
for x in range(height):
4447
for y in range(width):
45-
f = bfs(given, [[(x, y)]])
46-
found.extend(f)
47-
return len(found)
48+
# found.extend(bfs([[(x, y)]]))
49+
result += bfs([[(x, y)]])
50+
# return len(found)
51+
return result
4852

4953

5054
def part2(given: List[str]) -> int:
5155
def get_x_chars(letters, x, y):
5256
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)])
5357

5458
height, width = len(given), len(given[0])
55-
found = []
59+
# found = []
60+
result = 0
5661
for x, line in enumerate(given):
5762
for y, char in enumerate(line):
5863
if (
@@ -63,16 +68,14 @@ def get_x_chars(letters, x, y):
6368
# ..A.. OR ..A.. OR ..A.. OR ..A..
6469
# .S.S. .M.S. .M.M. .S.M.
6570
):
66-
found.append((x, y))
67-
return len(found)
71+
# found.append((x, y))
72+
result += 1
73+
# return len(found)
74+
return result
6875

6976

7077
# TEST
7178
def test():
72-
assert neighbors(0, 0, 2, 2) == [(0, 1), (1, 0), (1, 1)]
73-
assert neighbors(1, 1, 2, 2) == [(0, 0), (0, 1), (1, 0)]
74-
assert neighbors(1, 1, 3, 3) == [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)]
75-
7679
given = parser("""
7780
MMMSXXMASM
7881
MSAMXMSMSA

2024/benchmark-m1.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Benchmark 1: find 2024 -type f -regex ".*/[0-9]*\.py" | sort -n | xargs -L 1 python
2-
Time (mean ± σ): 133.3 ms ± 8.5 ms [User: 115.9 ms, System: 15.2 ms]
3-
Range (min … max): 128.4 ms … 156.5 ms 10 runs
2+
Time (mean ± σ): 134.9 ms ± 6.3 ms [User: 114.9 ms, System: 16.5 ms]
3+
Range (min … max): 127.5 ms … 147.1 ms 10 runs
44

0 commit comments

Comments
 (0)