Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numba
- def process1(s, part2=False): # Fast with numba.
- x1, x2, y1, y2 = parse.parse(
- 'target area: x={:d}..{:d}, y={:d}..{:d}', s).fixed
- @numba_njit(cache=True)
- def simulations(x1, x2, y1, y2):
- highest = -1000
- count = 0
- for dx0 in range(int(math.sqrt(x1)) if x1 > 0 else x1,
- (-int(math.sqrt(-x2)) if x2 < 0 else x2) + 1):
- for dy0 in range(y1, max(abs(y1), abs(y2)) + 1):
- dx, dy = dx0, dy0
- x = y = 0
- max_y = 0
- while ((x <= x2 or dx < 0) and (x >= x1 or dx > 0) and
- (y >= y1 or dy > 0)):
- x, y = x + dx, y + dy
- max_y = max(max_y, y)
- dx, dy = dx - np.sign(dx), dy - 1
- if y1 <= y <= y2 and x1 <= x <= x2:
- count += 1
- highest = max(highest, max_y)
- break
- return count if part2 else highest
- return simulations(x1, x2, y1, y2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement