Skip to content

Commit b955202

Browse files
committed
[2024/4] p2 solved
1 parent 14cf037 commit b955202

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

2024/4/script.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from GhostyUtils.vec2 import Dir, Vec2
44

55

6-
search_dirs = [
6+
xmas_dirs = [
77
Dir.EAST,
88
Dir.SOUTH_EAST,
99
Dir.SOUTH,
@@ -17,7 +17,7 @@
1717

1818
def search_xmas(grid: Grid, pos: Vec2) -> int:
1919
xmas_count = 0
20-
for dir_ in search_dirs:
20+
for dir_ in xmas_dirs:
2121
word = [grid[pos + dir_.as_vec2() * i]
2222
for i in range(4) if grid.in_bounds(pos + dir_.as_vec2() * i)]
2323
if word == list('XMAS'):
@@ -26,6 +26,14 @@ def search_xmas(grid: Grid, pos: Vec2) -> int:
2626
return xmas_count
2727

2828

29+
def search_masx(grid: Grid, pos: Vec2) -> int:
30+
one = sorted([grid[pos + Dir.NORTH_EAST], grid[pos + Dir.SOUTH_WEST]])
31+
two = sorted([grid[pos + Dir.NORTH_WEST], grid[pos + Dir.SOUTH_EAST]])
32+
if one == two == list('MS'):
33+
return 1
34+
return 0
35+
36+
2937
def main():
3038
grid = Grid(aoc.read_lines())
3139

@@ -38,6 +46,21 @@ def main():
3846
continue
3947
print("p1:", xmas_count)
4048

49+
masx_count = 0
50+
for y, row in enumerate(grid):
51+
# skip the top and bottom rows
52+
if y in [0, grid.height() - 1]:
53+
continue
54+
for x, cell in enumerate(row):
55+
# skip the left and right columns
56+
if x in [0, grid.width() - 1]:
57+
continue
58+
if cell == 'A':
59+
masx_count += search_masx(grid, Vec2(x, y))
60+
else:
61+
continue
62+
print("p2:", masx_count)
63+
4164

4265
if __name__ == "__main__":
4366
main()

0 commit comments

Comments
 (0)