Created
January 15, 2012 16:56
-
-
Save xpmatteo/1616428 to your computer and use it in GitHub Desktop.
Solution to Greed Ruby Koan
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
class Array | |
def occurrences_of(match) | |
self.select{ |number| match == number }.size | |
end | |
def delete_one(match) | |
for i in (0..size) | |
if match == self[i] | |
self.delete_at(i) | |
return | |
end | |
end | |
end | |
end | |
def single_die_rule(match, score, dice) | |
dice.occurrences_of(match) * score | |
end | |
def triple_rule(match, score, dice) | |
return 0 if dice.occurrences_of(match) < 3 | |
3.times { dice.delete_one match } | |
score | |
end | |
def score(dice) | |
triple_rule(1, 1000, dice) + | |
triple_rule(2, 200, dice) + | |
triple_rule(3, 300, dice) + | |
triple_rule(4, 400, dice) + | |
triple_rule(5, 500, dice) + | |
triple_rule(6, 600, dice) + | |
single_die_rule(1, 100, dice) + | |
single_die_rule(5, 50, dice) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment