Skip to content

Instantly share code, notes, and snippets.

@GrantJEmerson
Last active November 23, 2019 22:41
Show Gist options
  • Save GrantJEmerson/86fabf23bc4f1c6d2f77258763f1aaa0 to your computer and use it in GitHub Desktop.
Save GrantJEmerson/86fabf23bc4f1c6d2f77258763f1aaa0 to your computer and use it in GitHub Desktop.
Let's Build an Invisible Wall - SwiftMoji Entry #4
struct Item {
let name: String
let isNecessary: Bool
let price: Int
}
var ๐Ÿ›’ = [
Item(name: "๐Ÿ“ฑ", isNecessary: false, price: 600),
Item(name: "๐Ÿงป", isNecessary: true, price: 10),
Item(name: "๐ŸŽ", isNecessary: true, price: 5),
Item(name: "๐ŸŽฎ", isNecessary: false, price: 50),
Item(name: "๐Ÿ’ก", isNecessary: true, price: 25),
Item(name: "๐Ÿช", isNecessary: false, price: 7)
]
๐Ÿ›’.partition { !$0.isNecessary }
var money = 300
while !๐Ÿ›’.isEmpty && (money - ๐Ÿ›’[0].price) >= 0 {
let item = ๐Ÿ›’.removeFirst()
money -= item.price
print("Bought: \(item.name) for $\(item.price). Total: $\(money)")
}
// Bought: ๐Ÿ’ก for $25. Total: $275
// Bought: ๐Ÿงป for $10. Total: $265
// Bought: ๐ŸŽ for $5. Total: $260
// Bought: ๐ŸŽฎ for $50. Total: $210
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment