Created
December 21, 2020 09:15
-
-
Save felix-larsen/72e6b11d1e014ac66f565855c66dceff to your computer and use it in GitHub Desktop.
21th December solution - Advent of Code 2020 - swift
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
let allPotIngredients = allergiesToIngredients.map{ $0.value }.reduce(Set<String>()) { (all, set) -> Set<String> in | |
all.union(set) | |
} | |
let result = foods.map { $0.0.subtracting(allPotIngredients).count }.reduce(0, +) | |
print(result) |
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
var removedAValue = true | |
while removedAValue { | |
removedAValue = false | |
for (_, ingredients) in allergiesToIngredients { | |
if ingredients.count == 1 { | |
for a in allergiesToIngredients { | |
let countBefore = a.value.count | |
if a.value.count > 1 { | |
let filteredIngredients = a.value.filter { $0 != ingredients.first!} | |
allergiesToIngredients[a.key] = filteredIngredients | |
if countBefore != filteredIngredients.count { | |
removedAValue = true | |
} | |
} | |
} | |
} | |
} | |
} | |
let sortedByAllergyNames = allergiesToIngredients.sorted { (ele1, ele2) -> Bool in | |
ele1.key < ele2.key | |
} | |
print(sortedByAllergyNames.map { $0.value.first!}.joined(separator: ",")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment