Advertisement
hhoppe

Advent of code 2024 day 4

Dec 3rd, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def day4(s, part2=False):
  2.   grid = np.array([list(line) for line in s.splitlines()])
  3.   count = 0
  4.  
  5.   for _ in range(4):
  6.     grid = np.rot90(grid)
  7.     if not part2:
  8.       count += ((grid[:-3] == 'X') & (grid[1:-2] == 'M') &
  9.                 (grid[2:-1] == 'A') & (grid[3:] == 'S')).sum()
  10.       count += ((grid[:-3, :-3] == 'X') & (grid[1:-2, 1:-2] == 'M') &
  11.                 (grid[2:-1, 2:-1] == 'A') & (grid[3:, 3:] == 'S')).sum()
  12.     else:
  13.       count += ((grid[:-2, :-2] == 'M') & (grid[:-2, 2:] == 'M') &
  14.                 (grid[2:, :-2] == 'S') & (grid[2:, 2:] == 'S') &
  15.                 (grid[1:-1, 1:-1] == 'A')).sum()
  16.  
  17.   return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement