Skip to content

Instantly share code, notes, and snippets.

@anchietajunior
Last active May 23, 2020 19:59
Show Gist options
  • Save anchietajunior/2f3e026fe684a1754b9dbb916dda4ce2 to your computer and use it in GitHub Desktop.
Save anchietajunior/2f3e026fe684a1754b9dbb916dda4ce2 to your computer and use it in GitHub Desktop.
Implementation and CLI
puts "Type the cooking time: "
cooking_time = gets.chomp.to_i
puts "Type the first hourglass time: "
hour_glass_one = gets.chomp.to_i
puts "Type the second hourglass time: "
hour_glass_two = gets.chomp.to_i
def calculate_minimum_time(cooking_time, hour_glass_one, hour_glass_two)
minimum_time = 0
# Set initial current times
current_time_one, current_time_two = hour_glass_one, hour_glass_two
current_times_difference= nil
possible_cooking = false
while current_times_difference!= 0
if current_time_one > current_time_two
current_times_difference = current_time_one - current_time_two
minimum_time += current_time_two
current_time_one = current_times_difference
current_time_two = hour_glass_two
else
current_times_difference = current_time_two - current_time_one
minimum_time += current_time_one
current_time_two = current_times_difference
current_time_one = hour_glass_one
end
if current_times_difference == cooking_time
minimum_time += cooking_time
possible_cooking = true
break
end
end
return "Minimum time: #{minimum_time} minutes." if possible_cooking
"Can't cook exactly with these times."
end
p calculate_minimum_time(cooking_time, hour_glass_one, hour_glass_two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment