Last active
August 29, 2015 14:18
-
-
Save ybur-yug/70624158c99ebba9550e 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
time = 5 | |
car_positions = [1, 1, 1] | |
while time >= 0 do | |
time = time -1; puts # newline | |
car_positions.each_with_index do |pos, index| | |
if Random.rand > 0.3 | |
car_positions[index] += 1 | |
end | |
print '-' * car_positions[index];puts # newline | |
end | |
end | |
VS | |
def move_cars(car_positions, time) | |
{ cars: car_positions.map { |pos| pos += 1 ? Random.rand > 0.3 : pos }, | |
time: time - 1 } | |
end | |
def output_car car_position | |
puts '-' * car_position | |
end | |
def run_step_of_race state | |
move_cars(state[:cars], state[:time]) | |
end | |
def draw car_positions | |
car_positions.each { |c| output_car c } | |
end | |
def race car_positions, time | |
draw car_positions | |
state = move_cars car_positions, | |
race(run_step_of_race(state)[:cars],run_step_of_race(state)[:time]) ? state[:time] >= 0 | |
end | |
race [1, 1, 1], 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment