Advertisement
hhoppe

Advent of code 2023 day 11 fastest

Dec 11th, 2023 (edited)
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. def day11(s, *, part2=False):
  2.   expand = 1_000_000 if part2 else 2
  3.   grid = np.array([list(line) for line in s.splitlines()])
  4.   coords = np.argwhere(grid == '#').T
  5.  
  6.   def process(values):
  7.     total = last_value = num = 0
  8.     for value, group in itertools.groupby(values):
  9.       spacing = 1 + (value - last_value - 1) * expand
  10.       total += num * (len(values) - num) * spacing
  11.       num += len(list(group))
  12.       last_value = value
  13.     return total
  14.  
  15.   return process(coords[0]) + process(sorted(coords[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement