Executing the code:
ruby noodle_timer.rb 3 5 7
The arguments is in order, the time to cook noodles and the time of each hour glasses
time_to_cook = ARGV[0].to_i | |
hour_glass_one = ARGV[1].to_i | |
hour_glass_two = ARGV[2].to_i | |
def can_cook?(time_to_cook, hour_glass_one, hour_glass_two) | |
total_time = 0 | |
used_hour_glass_one = hour_glass_one | |
used_hour_glass_two = hour_glass_two | |
time_difference = nil | |
can_cook = false | |
while time_difference != 0 | |
if used_hour_glass_one > used_hour_glass_two | |
time_difference = used_hour_glass_one - used_hour_glass_two | |
total_time += used_hour_glass_two | |
used_hour_glass_one = time_difference | |
used_hour_glass_two = hour_glass_two | |
else | |
time_difference = used_hour_glass_two - used_hour_glass_one | |
total_time += used_hour_glass_one | |
used_hour_glass_two = time_difference | |
used_hour_glass_one = hour_glass_one | |
end | |
if time_difference == time_to_cook | |
total_time += time_to_cook | |
can_cook = true | |
break | |
end | |
end | |
return total_time if can_cook | |
return false | |
end | |
result = can_cook?(time_to_cook, hour_glass_one, hour_glass_two) | |
if result | |
puts "Tempo mínimo necessário para o miojo ficar pronto são #{result} minutos" | |
else | |
puts 'Não vai ser possível cozinhar no tempo exato com estas ampulhetas' | |
end |