Skip to content

Instantly share code, notes, and snippets.

@jeanfbrito
Last active May 20, 2020 15:27
Show Gist options
  • Save jeanfbrito/131854eaf68dd220576db67dd47ffd45 to your computer and use it in GitHub Desktop.
Save jeanfbrito/131854eaf68dd220576db67dd47ffd45 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment