Skip to content

Instantly share code, notes, and snippets.

@keithrozario
Created February 21, 2025 01:36
Show Gist options
  • Save keithrozario/47cbc9fdc99271e68cd35f87e40af81b to your computer and use it in GitHub Desktop.
Save keithrozario/47cbc9fdc99271e68cd35f87e40af81b to your computer and use it in GitHub Desktop.
Solve Question 13 with matrices
def calculate_pushes(a_x, b_x, a_y, b_y, prize_x, prize_y) -> tuple[int, int]:
"""
Using matrix multiplication
if A . [x,y] = [prize_x, prize_y]
then [x,y] = A^-1 . [prize_x, prize_y[]
"""
A = np.array([[a_x, b_x], [a_y, b_y]])
P = np.array([prize_x, prize_y])
a_pushes, b_pushes = np.linalg.solve(A, P).round().astype(int)
## double check integer versions work
if (a_x*a_pushes + b_x*b_pushes) == prize_x and (a_y*a_pushes + b_y*b_pushes) == prize_y:
return (a_pushes, b_pushes)
else:
return (None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment