Skip to content

Instantly share code, notes, and snippets.

@aherrman
Created December 10, 2009 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aherrman/253634 to your computer and use it in GitHub Desktop.
Save aherrman/253634 to your computer and use it in GitHub Desktop.
First attempt at ruby koans' Greed score calculator
def score(dice)
count_dice_types(dice).each_pair.inject(0) { |score, pair|
value, count = pair
score + score_dice_value(value, count)
}
end
def score_dice_value(value, count)
return 0 if count == 0
return tripple_score(value) + score_dice_value(value, count - 3) if count > 2
return 100 * count if value == 1
return 50 * count if value == 5
0
end
def tripple_score(value)
return 1000 if value == 1
value * 100
end
def count_dice_types(dice)
dice.inject({}) { |counts, value|
counts[value] = 0 if counts[value].nil?
counts[value] += 1
counts
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment