Welcome to MindCipher, a social repository of the world's greatest brain teasers, logic puzzles and mental challenges.

Yoshi Gillaspie

Likes 0

Yoshi Gillaspie must be new here...

Ratings 0

No ratings yet!

Comments 1

Charlie and the Chocolate Factory

A ruby solution. You could also build in different wrapper trade in ratios or different costs of chocolate bars.

class Buyer attrreader :chocbars def initialize(cash = 45, choc_bars = 0)

@cash = cash
@choc_bars = choc_bars

end

def getdatchocolate

spend_monies
trade_in_wrappers

end

private def tradeinwrappers

wrappers = @choc_bars
until wrappers <= 2
  # 3 for 1 more bar w/wrapper: 3 - 1 = 2
  wrappers -= 2
  @choc_bars += 1
end
@choc_bars

end

def spend_monies

@choc_bars += (@cash / 3)
@cash -= (@cash % 3)

end end

x = Buyer.new x.getdatchocolate

context