Created
September 8, 2011 14:23
-
-
Save peter/1203514 to your computer and use it in GitHub Desktop.
Solution to Ruby Koans Scoring Project (about_scoring_project.rb)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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