01 May 2012
Ruby Koans has sat on my todo list for almost as long as I’ve been a Ruby developer and this week I’ve finally taken it upon myself to tackle it. Expectedly, there’s little new to be expected for anyone who has served their time as a developer, but there’s plenty of helpful tricks and Ruby intricacies to be reminded of. I’ve enjoyed looking at how others have tackled the problem, so assuming you’ve already given the question a good attempt, here’s my solution:
def score(dice)
score = 0
1.upto(6).each do |num|
amount = dice.count(num)
if amount >= 3
score += num == 1 ? 1000 : num * 100
amount -= 3
end
score += 100 * amount if num == 1
score += 50 * amount if num == 5
end
score
end