Advertisement
hhoppe

Advent of code 2024 day 13 numpy fully vectorized

Dec 13th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. def day13(s, *, part2=False):
  2.   values = np.array(
  3.       [[re.findall(r'\d+', line) for line in s2.splitlines()] for s2 in s.split('\n\n')], int
  4.   )
  5.   b = values[:, 2][..., None] + (10_000_000_000_000 if part2 else 0)
  6.   matrix = np.moveaxis(values[:, :2], 1, 2)
  7.   x = np.linalg.solve(matrix, b)
  8.   rounded = (x + 0.5).astype(int)
  9.   solved = (matrix @ rounded == b).all(1)[:, 0]
  10.   return np.sum(rounded[solved][..., 0] @ [3, 1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement