Created
December 19, 2021 04:33
-
-
Save albertein/893b4a73eaa2d6c2b8a13ecf8fc72a1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def simulate(dx, dy, x1, x2, y1, y2): | |
x, y = (0, 0) | |
initial_dx, initial_dy = dx, dy | |
while True: | |
x += dx | |
y += dy | |
dy -= 1 | |
if dx < 0: | |
dx += 1 | |
elif dx > 0: | |
dx -= 1 | |
if x >= x1 and x <= x2 and y >= y1 and y <= y2: | |
return True | |
if x < x1 or x > x2: | |
if dx == 0: | |
# If dx is 0 and not in range, we'll never hit the target. | |
return False | |
if x > x2 or y < y1: | |
return False | |
return False | |
def find_best_dy(dx, x1, x2, y1, y2): | |
dy = 999 | |
have_found_hit = False | |
count = 0 | |
while dy > -999: | |
hit = simulate(dx, dy, x1, x2, y1, y2) | |
if hit: | |
have_found_hit = True | |
count += 1 | |
dy -= 1 | |
return count | |
def find_trajectory(x1, x2, y1, y2): | |
x1, x2 = sorted([x1, x2]) | |
y1, y2 = sorted([y1, y2]) | |
# Initial dx must be at most x2, otherwise it'll fall out of bounds. | |
dx = x2 | |
best_solution = -999999 | |
best_dy = 0 | |
best_dx = 0 | |
count = 0 | |
while dx > 0: | |
count += find_best_dy(dx, x1, x2, y1, y2) | |
dx -= 1 | |
print(count) | |
find_trajectory(25, 67, -260, -200) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment