DEV Community

Discussion on: Daily Challenge #41 - Greed is Good

Collapse
 
hanachin profile image
Seiei Miyagi

ruby <3

def score(values)
  values.tally.sum do |n, cnt, acc = 0|
    case [n, cnt]
    in 1, 3.. then
      cnt -= 3
      acc += 1000
      redo
    in 6, 3.. then
      cnt -= 3
      acc += 600
      redo
    in 5, 3.. then
      cnt -= 3
      acc += 500
      redo
    in 4, 3.. then
      cnt -= 3
      acc += 400
      redo
    in 3, 3.. then
      cnt -= 3
      acc += 300
      redo
    in 2, 3.. then
      cnt -= 3
      acc += 200
      redo
    in 1, 1.. then
      cnt -= 1
      acc += 100
      redo
    in 5, 1.. then
      cnt -= 1
      acc += 50
      redo
    else
      acc
    end
  end
end

p score([5, 1, 3, 4, 1]) # => 50 + 2 * 100 = 250
p score([1, 1, 1, 3, 1]) # => 1000 + 100 = 1100
p score([2, 4, 4, 5, 4]) # => 400 + 50 = 450