Created
January 27, 2012 17:43
-
-
Save martin-ueding/1689964 to your computer and use it in GitHub Desktop.
running dog with alarm clock
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Copyright (c) 2012 Martin Ueding <[email protected]> | |
""" | |
Based on a Question from "Physics - Stack Exchange". | |
http://physics.stackexchange.com/questions/20083/riddle-about-speed | |
""" | |
# Position, speed. | |
dog = [0, 5] | |
# The timer on the dog's back. | |
timer = 0 | |
# Global time. | |
t = 0 | |
# Time interval for simulation. | |
dt = 0.0001 | |
# List of waves. | |
waves = [] | |
# Count of iterations. | |
i = 0 | |
while True: | |
if timer > 10: | |
timer -= 10 | |
# Spawn a new wave a meter behind the dog. | |
waves.append([dog[0]-1, 340]) | |
in_front = 0 | |
for wave in waves: | |
# Check whether the wave is front of the dog. | |
in_front_of_dog = wave[0] > dog[0] | |
# Delete waves that are behind the dog and slower than the dog. Those | |
# waves could never reach the dog anyway. | |
if not in_front_of_dog: | |
if wave[1] < dog[1]: | |
waves.remove(wave) | |
# Move the wave. | |
wave[0] += wave[1] * dt | |
# Check whether the wave is still in front of the (now moved) dog. | |
still_in_front_of_dog = wave[0] > (dog[0] + dog[1]*dt) | |
# Count the number of waves that are still in front of the dog. | |
if still_in_front_of_dog: | |
in_front += 1 | |
# If the wave passed the dog, he would head it and therefore double his | |
# speed. | |
if in_front_of_dog ^ still_in_front_of_dog: | |
if in_front_of_dog: | |
print "Dog catches wave." | |
else: | |
print "Wave catches dog." | |
print "t = %.1f, position = %.1f, speed = %.1f, waves in front = %d" % (t, dog[0], dog[1], in_front) | |
print "Waves at", ', '.join(["%.1f" % x for x in sorted([x[0] for x in waves])]) | |
dog[1] *= 2 | |
# Move the dog. | |
dog[0] += dog[1] * dt | |
# Increase the time. | |
timer += dt | |
t += dt | |
i += 1 | |
# Stop when there are no waves in front of the dog left. The t > 100 is a | |
# safeguard before the first alarm goes off. | |
if t > 100 and len(waves) == 0: | |
break | |
print "Final speed of the dog:", dog[1] |
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
Wave catches dog. | |
t = 10.0, position = 50.0, speed = 5.0, waves in front = 1 | |
Waves at 50.0 | |
Wave catches dog. | |
t = 20.0, position = 150.0, speed = 10.0, waves in front = 2 | |
Waves at 150.0, 3450.1 | |
Wave catches dog. | |
t = 30.0, position = 350.0, speed = 20.0, waves in front = 3 | |
Waves at 350.0, 3550.1, 6850.1 | |
Wave catches dog. | |
t = 40.0, position = 750.0, speed = 40.0, waves in front = 4 | |
Waves at 750.1, 3750.1, 6950.1, 10250.2 | |
Wave catches dog. | |
t = 50.0, position = 1550.1, speed = 80.0, waves in front = 5 | |
Waves at 1550.1, 4150.2, 7150.3, 10350.3, 13650.3 | |
Wave catches dog. | |
t = 60.0, position = 3150.3, speed = 160.0, waves in front = 6 | |
Waves at 3150.4, 4950.7, 7550.8, 10550.9, 13750.9, 17050.9 | |
Wave catches dog. | |
t = 60.0, position = 3150.5, speed = 320.0, waves in front = 6 | |
Waves at 3150.5, 4950.8, 7550.9, 10551.0, 13751.0, 17051.0 | |
Dog catches wave. | |
t = 66.0, position = 6991.1, speed = 640.0, waves in front = 4 | |
Waves at 6991.1, 9591.3, 12591.3, 15791.4, 19091.4 | |
Dog catches wave. | |
t = 68.8, position = 10531.6, speed = 1280.0, waves in front = 3 | |
Waves at 10531.7, 13531.8, 16731.8, 20031.8 | |
Dog catches wave. | |
t = 70.1, position = 13991.2, speed = 2560.0, waves in front = 2 | |
Waves at 13991.3, 17191.3, 20491.3 | |
Dog catches wave. | |
t = 70.8, position = 17418.5, speed = 5120.0, waves in front = 1 | |
Waves at 17418.9, 20718.9 | |
Dog catches wave. | |
t = 71.1, position = 20831.5, speed = 10240.0, waves in front = 0 | |
Waves at 20832.2 | |
Final speed of the dog: 20480 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment