Advertisement
hhoppe

Advent of code 2021 day 17

Dec 16th, 2021 (edited)
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import numba
  2.  
  3. def process1(s, part2=False):  # Fast with numba.
  4.   x1, x2, y1, y2 = parse.parse(
  5.       'target area: x={:d}..{:d}, y={:d}..{:d}', s).fixed
  6.  
  7.   @numba_njit(cache=True)
  8.   def simulations(x1, x2, y1, y2):
  9.     highest = -1000
  10.     count = 0
  11.     for dx0 in range(int(math.sqrt(x1)) if x1 > 0 else x1,
  12.                      (-int(math.sqrt(-x2)) if x2 < 0 else x2) + 1):
  13.       for dy0 in range(y1, max(abs(y1), abs(y2)) + 1):
  14.         dx, dy = dx0, dy0
  15.         x = y = 0
  16.         max_y = 0
  17.         while ((x <= x2 or dx < 0) and (x >= x1 or dx > 0) and
  18.                (y >= y1 or dy > 0)):
  19.           x, y = x + dx, y + dy
  20.           max_y = max(max_y, y)
  21.           dx, dy = dx - np.sign(dx), dy - 1
  22.           if y1 <= y <= y2 and x1 <= x <= x2:
  23.             count += 1
  24.             highest = max(highest, max_y)
  25.             break
  26.     return count if part2 else highest
  27.  
  28.   return simulations(x1, x2, y1, y2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement