Skip to content

Instantly share code, notes, and snippets.

@joshuastr
Created April 30, 2016 20:02
Show Gist options
  • Save joshuastr/451836f3ef92e92d81d92d4c8d2df609 to your computer and use it in GitHub Desktop.
Save joshuastr/451836f3ef92e92d81d92d4c8d2df609 to your computer and use it in GitHub Desktop.
Pop Bottles
# You've just been hired at Lighthouse Markets, a reputable (and thoroughly fictional) grocery store chain.
# One of the primary features of Lighthouse Markets is their recycling program on pop bottles. Here is how the program works:
# For every two empty bottles, you can get one free (full) bottle of pop
# For every four bottle caps, you can get one free (full) bottle of pop
# Each bottle of pop costs $2 to purchase
# Given these parameters, write a program so that you can figure out how many total bottles of pop can be redeemed given a customer investment.
p 'Welcome to Lighthouse Pop Bottle Recycling Program, Bottles are $2 each, how many would you like to purchase?'
init_investment = gets.chomp.to_i
# def caps(dollar_amount)
# @caps = dollar_amount / 4
# end
# def bottles(dollar_amount)
# @bottles = dollar_amount / 2
# end
def recycled_bottles(bottles)
bottles / 2
end
def recycled_caps(caps)
caps / 4
end
def extra_bottles(bottles)
bottles % 2
end
def extra_caps(caps)
caps % 4
end
# def total_bottles(dollars)
# bottles = dollars / 2
# end
dollar_amount = init_investment
total_bottles = dollar_amount / 2
total_caps = dollar_amount / 2
redeemed_bottles = total_bottles
while (total_bottles >= 2 || total_caps >= 4)
new_bottles_from_bottles = recycled_bottles(total_bottles)
new_bottles_from_caps = recycled_caps(total_caps)
remaining_bottles = extra_bottles(total_bottles)
remaining_caps = extra_caps(total_caps)
total_bottles = new_bottles_from_bottles + remaining_bottles + new_bottles_from_caps
total_caps = new_bottles_from_caps + remaining_caps + new_bottles_from_bottles
redeemed_bottles += (new_bottles_from_bottles + new_bottles_from_caps)
end
puts redeemed_bottles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment