-
-
Save Dispader/5ae1e102ae2a8dbd2e8e to your computer and use it in GitHub Desktop.
class Greed | |
def self.score(dice) | |
# initialize a score | |
score = 0 | |
# create a hash of the roll (face values) and the number of times rolled | |
rolls = dice.inject(Hash.new(0)) {|hash, roll| hash[roll] += 1; hash } | |
# for each set of 1s, add 1000 points to the score | |
if rolls[1]/3 > 0 | |
score += ( rolls[1]/3 ) * 1000 # award 1000 points per set of 1s | |
rolls[1] -= ( rolls[1]/3 ) * 3 # remove each set from the roll count | |
end | |
# for each other set, add 100 points per set face value | |
(2..6).each { |roll| | |
if rolls[roll]/3 > 0 | |
score += ( rolls[roll]/3 ) * ( 100 * roll) # award 100 * roll per set | |
rolls[roll] -= ( rolls[roll]/3 ) * 3 # remove sets from count | |
end | |
} | |
# TODO: FIX dry violation for above two methods | |
# for each (single) roll that has a pont value, award points to the score | |
{ 1 => 100, 5 => 50 }.each { |roll, points_per_roll| | |
score += points_per_roll * rolls[roll] | |
} | |
# yield the score | |
score | |
end | |
end |
@Dispader Holy crap am I getting old: http://stackoverflow.com/questions/4749973/ruby-greed-koan-how-can-i-improve-my-if-then-soup/13552277#13552277
I really did not remember this and this solution is not like the one I just wrote...How many times have I written greed in my life? I have no idea :)
@enebo Har— and I like all your approaches better than my first whack at it. I'm going through this weird re-transition, going back to getting my (client/caller) code functional.
Have you had a look at http://exercism.io/ yet? Pretty cool for the morning pushups.
(Hrm— and GitHub doesn't seem to notify me when I'm mentioned in Gists. Just happened to see your comment, there.)
@Dispader ... nor me :) I have not checked exercism but I have a tab open and will check it out.
I just saw your irb/completion thing on G+ and noticed this brown bag problem. Here is my soln. I feel it has some good aspects but it is somewhat golfed (although I could have killed it if I used enumerator and mixed scrore in):