question.rb
solution.rb
playground.rb
1 require_relative "../physics" 2 3 question :the_drunkards_walk do 4 kind :scq 5 describes "A drunkard in a narrow lane takes %{forward} steps forward and %{back} back, again and again, each step " \ 6 "%{step} long and %{step_time} long. How long before he falls into a pit %{pit} ahead?" 7 given forward: 5 8 given back: 3 9 given step: "1 m" 10 given step_time: "1 s" 11 given pit: "13 m" 12 13 option "37 s" 14 option "39 s" 15 option "52 s" 16 option "65 s" 17 end
1 require_relative "../questions/the_drunkards_walk" 2 require_relative "../science/motion_in_a_straight_line/velocity" 3 4 solve :the_drunkards_walk , using: Velocity do 5 take step: :Delta_x , step_time: :Delta_t , pit: :x 6 assume x_0: "0 m" 7 step = value_of ( :Delta_x ) 8 tick = value_of ( :Delta_t ) 9 pit = value_of ( :x ) 10 forward = find_value :v_av , given: { Delta_x : step , Delta_t : tick } 11 back = find_value :v_av , given: { Delta_x : - step , Delta_t : tick } 12 rhythm = [ forward ] * value_of ( :forward ) + [ back ] * value_of ( :back ) 13 x = value_of ( :x_0 ) 14 t = 0 15 rhythm . cycle do | v | 16 x = find_value :x , given: { x_0: x , v_av: v , Delta_t : tick } 17 t += tick 18 break if x >= pit 19 end 20 21 answer t 22 23 conclusion "Following him second by second is the point: averaging his progress would miss that " \ 24 "he gets there at the end of a forward stretch, before he can step back." 25 end
1 require_relative "../solutions/the_drunkards_walk" 2 3 play_with :the_drunkards_walk do 4 title "The Drunkard's Walk" 5 end