Skip to content

Instantly share code, notes, and snippets.

@xpmatteo
Created January 15, 2012 16:56
Show Gist options
  • Save xpmatteo/1616428 to your computer and use it in GitHub Desktop.
Save xpmatteo/1616428 to your computer and use it in GitHub Desktop.
Solution to Greed Ruby Koan
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