Skip to content

Instantly share code, notes, and snippets.

@cirops
Created May 24, 2018 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cirops/bc8e6216d9b7ea75c8d06e61fe77726a to your computer and use it in GitHub Desktop.
Save cirops/bc8e6216d9b7ea75c8d06e61fe77726a to your computer and use it in GitHub Desktop.
Calculates Ideal Miojo Cooking time with two hourglasses
# Solves the "Miojo with 2 hourglasses" problem
# cooking time and both hourglass times are passed as arguments, as in the following sample:
# ruby miojoproblem.rb 3 5 7
# returns the minimum total time to cook the miojo accurately, or a fail message if not possible
# Solution is ad-hoc, can probably be optimized a lot
raise "Must specify 3 inputs: Miojo cooking time, hourglass 1 time, hourglass 2 time." if ARGV.length != 3
cook_time = ARGV[0].to_i
hg_1 = ARGV[1].to_i
hg_2 = ARGV[2].to_i
raise "Cooking time must be smaller than both hourglasses' times." if cook_time >= hg_1 or cook_time >= hg_2
if hg_1 % 2 == 0 and hg_2 % 2 == 0 and cook_time % 2 != 0
puts "Not possible to calculate odd cooking time with even time hourglasses!"
return
end
i = cook_time
j = cook_time
maximum = [cook_time, hg_1, hg_2].max * 100
for i in (hg_1..maximum).step(hg_1) do
for j in (hg_2..maximum).step(hg_2) do
if (i - j).abs == cook_time
puts "Minimum cooking time: #{[i, j].max} minutes."
return
end
end
end
puts "Cooking time not possible to calculate with the given hourglasses";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment