Skip to content

Instantly share code, notes, and snippets.

@peter
Created September 8, 2011 14:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/1203514 to your computer and use it in GitHub Desktop.
Save peter/1203514 to your computer and use it in GitHub Desktop.
Solution to Ruby Koans Scoring Project (about_scoring_project.rb)
def counts_from_dice(dice)
dice.inject({}) do |hash, value|
hash[value] ||= 0
hash[value] += 1
hash
end
end
def score(dice)
counts_from_dice(dice).inject(0) do |sum, (number, count)|
if count >= 3
sum += (number == 1 ? 1000 : number * 100)
extras_count = count - 3
else
extras_count = count
end
case number
when 1
sum += 100*extras_count
when 5
sum += 50*extras_count
else
sum += 0
end
sum
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment