Skip to content

Instantly share code, notes, and snippets.

@cody-code-wy
Last active May 28, 2016 00:33
Show Gist options
  • Save cody-code-wy/6571cbf116ccbd0a23fea309303bfe7c to your computer and use it in GitHub Desktop.
Save cody-code-wy/6571cbf116ccbd0a23fea309303bfe7c to your computer and use it in GitHub Desktop.
#takes a dollar ammount and return the number of purchased bottles plus the maximum number of bottles possible with recyling
def total_bottles_from_purchase(amt)
amt = amt / 2 #get the number of purchased bottles
#Track the bottles, and caps not redeemed. Track the number of purchased, and free bottles recieved
tracker = {bottles:amt,caps:amt,purchased:amt,free:0}
while (tracker[:bottles] >= 2) || (tracker[:caps] >= 4)
#get the number of new free bottles from recycling
free = tracker[:bottles] / 2
free += tracker[:caps] / 4
#remove reedemed bottles and caps
tracker[:bottles] %= 2
tracker[:caps] %= 4
#put the new bottles in to tracker
tracker[:free] += free
tracker[:bottles] += free
tracker[:caps] += free
end
#Add the total number of bottles recieved
tracker[:total] = tracker[:purchased] + tracker[:free]
return tracker
end
while true do
puts "Put a dolar amount (eg: 20) to find your savings, or exit to stop"
user = gets().chomp.strip.downcase
puts case user
when "exit"
puts "Exiting"
return
when /\d/
save = total_bottles_from_purchase(user.to_i)
puts "You would have #{save[:total]} bottles, only #{save[:purchased]} of which you paid for!\nThat means you got #{save[:free]} bottles for free!\nYou still have #{save[:caps]} Bottle caps, and #{save[:bottles]} bottles to recycle, if you buy some more bottles you can recycle these!"
else
puts "I couldn't understand your input, sorry."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment