0hc.net Menu
Home Overview
About Menu
Impressum

The Polar Bear Problem

The puzzle works with two dice: "In Greenland bears sitting around holes and fishing. Under the ice are fish." This means 2 and 3 are: 1 hole, 2 bears, 4 fish Try solving before looking at the solution... Ruby: class Groenland def initialize(x) @a, @b, @c = 0, 0, 0 @g = x + 1 end def f(xs) xs.each{|x| f1(x) if x%2 != 0} return [@a, @b, @c] end private def f1(x) @a += 1 @b += x-1 @c += @g-x end end def get_i(str) print str gets.to_i end def put(as) as.each{|a| print a, " "} print "\n" end c = [] a = get_i "Number of dice: " b = get_i "Sides each die: " (1..a).each{|i| c.push(get_i("Die number #{i}: "))} put c + ["=>"] + Groenland.new(b).f(c)
0hc.net    © 2001-2014 Harald Wolfsgruber