Advertisement
hhoppe

Advent of code 2023 day 11 fast

Dec 11th, 2023
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. def day11(s, *, part2=False):
  2.   growth = 999_999 if part2 else 1
  3.   grid = np.array([list(line) for line in s.splitlines()])
  4.   galaxies = np.argwhere(grid == '#')
  5.   all_g1_g2 = itertools.combinations(galaxies, 2)
  6.   total = sum(abs(a - b) for g1, g2 in all_g1_g2 for a, b in zip(g1, g2))
  7.   for c in range(2):
  8.     for i in range(len(grid)):
  9.       if np.all(grid[i] == '.'):
  10.         num_before = sum(1 for galaxy in galaxies if galaxy[c] < i)
  11.         total += growth * num_before * (len(galaxies) - num_before)
  12.     grid = grid.T
  13.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement