Last active
November 23, 2019 22:41
-
-
Save GrantJEmerson/86fabf23bc4f1c6d2f77258763f1aaa0 to your computer and use it in GitHub Desktop.
Let's Build an Invisible Wall - SwiftMoji Entry #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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