Skip to content

Instantly share code, notes, and snippets.

@ShiangYong
Created December 14, 2016 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShiangYong/d9e71a530807102df298d113cbfa2a7c to your computer and use it in GitHub Desktop.
Save ShiangYong/d9e71a530807102df298d113cbfa2a7c to your computer and use it in GitHub Desktop.
Python finite difference code to solve the Angry Ram puzzle, http://fivethirtyeight.com/features/can-you-outrun-the-angry-ram-coming-right-for-oh-god/
import math
import matplotlib.pyplot as plot
def generate_path(speed, num_steps):
scaled_step = speed/num_steps
step = 1.0/num_steps
ram_x = [0] * (num_steps + 1)
ram_y = [0] * (num_steps + 1)
for i in range(num_steps):
dx = 1.0 - ram_x[i]
dy = i*step - ram_y[i]
hypotenuse = math.hypot(dx, dy)
ram_x[i + 1] = ram_x[i] + scaled_step * dx / hypotenuse
ram_y[i + 1] = ram_y[i] + scaled_step * dy / hypotenuse
print(math.hypot(ram_x[-1] - 1.0, ram_y[-1] - 1.0))
#plot.scatter(ram_x, ram_y)
#plot.xlim(0.0,1.0)
#plot.ylim(0.0, 1.0)
#plot.show()
return math.hypot(ram_x[-1] - 1.0, ram_y[-1] - 1.0)
# print(generate_path(1.0, 1000))
generate_path(1.6, 1000*1000)
generate_path(1.61, 1000*1000)
generate_path(1.615, 1000*1000)
generate_path(1.617, 1000*1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment