Skip to content

Instantly share code, notes, and snippets.

@seanlerner
Last active April 24, 2017 15:37
Show Gist options
  • Save seanlerner/1f3ea6ebfe6b2f40399cea058f1e3c4b to your computer and use it in GitHub Desktop.
Save seanlerner/1f3ea6ebfe6b2f40399cea058f1e3c4b to your computer and use it in GitHub Desktop.
Gilded Rose Ruby Solution
class GildedRose
attr_reader :name, :days_remaining, :quality
∞ = Float::INFINITY
CHEESE = {
'Normal Item' => { -∞..0 => -2, 1..∞ => -1 },
'Aged Brie' => { -∞..0 => 2, 1..∞ => 1 },
'Conjured Mana Cake' => { -∞..0 => -4, 1..∞ => -2 },
'Backstage passes to a TAFKAL80ETC concert' => { -∞..0 => -∞, 1..5 => 3, 6..10 => 2, 10..∞ => 1 }
}
def initialize(name:, days_remaining:, quality:)
@name = name
@days_remaining = days_remaining
@quality = quality
end
def tick
return if name == 'Sulfuras, Hand of Ragnaros'
@quality += CHEESE[name].select { |range| range === days_remaining }.values.first
@quality = quality.clamp(0, 50)
@days_remaining -= 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment