Skip to content

Instantly share code, notes, and snippets.

@noahbass
Created February 16, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahbass/9d5b6d84fbd6a0e19bae89ac365cc312 to your computer and use it in GitHub Desktop.
Save noahbass/9d5b6d84fbd6a0e19bae89ac365cc312 to your computer and use it in GitHub Desktop.
import XCTest
typealias Quality = Int
typealias SellIn = Int
typealias UpdateRule = (Quality, SellIn) -> Quality
enum ItemType {
case normal
case sulfuras
case brie
case backstage
var updateRule: UpdateRule {
switch self {
case .normal:
return { quality, _ in max(0, quality-1) }
case .sulfuras:
return { quality, _ in quality }
case .brie:
return { quality, _ in min(quality+1, 50) }
case .backstage:
return { quality, sellin in }
}
}
}
// GlidedRose.swift
public class GildedRose {
var items: [Item]
required public init(items: [Item]) {
self.items = items
}
public func updateQuality() {
for i in 0..<items.count {
if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
if (items[i].quality > 0) {
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
items[i].quality = items[i].quality - 1
}
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") {
if (items[i].sellIn < 11) {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
}
}
if (items[i].sellIn < 6) {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
}
}
}
}
}
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
items[i].sellIn = items[i].sellIn - 1
}
if (items[i].sellIn < 0) {
if (items[i].name != "Aged Brie") {
if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
if (items[i].quality > 0) {
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
items[i].quality = items[i].quality - 1
}
}
} else {
items[i].quality = items[i].quality - items[i].quality
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
}
}
}
}
}
}
// Item.swift
public struct Item {
public var name: String
public var sellIn: Int
public var quality: Int
public init(name: String, sellIn: Int, quality: Int) {
self.name = name
self.sellIn = sellIn
self.quality = quality
}
}
extension Item: CustomStringConvertible {
public var description: String {
return self.name + ", " + String(self.sellIn) + ", " + String(self.quality);
}
}
// main.swift
let items = [
Item(name: "+5 Dexterity Vest", sellIn: 10, quality: 20),
Item(name: "Aged Brie", sellIn: 2, quality: 0),
Item(name: "Elixir of the Mongoose", sellIn: 5, quality: 7),
Item(name: "Sulfuras, Hand of Ragnaros", sellIn: 0, quality: 80),
Item(name: "Sulfuras, Hand of Ragnaros", sellIn: -1, quality: 80),
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 15, quality: 20),
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 10, quality: 49),
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 5, quality: 49),
// this conjured item does not work properly yet
Item(name: "Conjured Mana Cake", sellIn: 3, quality: 6)
]
let app = GildedRose(items: items);
var days = 2;
if (CommandLine.argc > 1) {
days = Int(CommandLine.arguments[1])! + 1
}
for i in 0..<days {
print("-------- day \(i) --------");
print("name, sellIn, quality");
for item in items {
print(item);
}
print("");
app.updateQuality();
}
func testFoo() {
let items = [Item(name: "foo", sellIn: 0, quality: 0)]
let app = GildedRose(items: items);
app.updateQuality();
// XCTAssertEqual("fixme", app.items[0].name);
}
testFoo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment