Slight refactor

This commit is contained in:
Max Nuding 2021-12-06 20:50:41 +01:00
parent 85cfa9003d
commit c14ff89c45
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

@ -22,20 +22,19 @@ struct Day06: Runnable {
}
func run(fish: [Int], numDays: Int) {
var tmpFish = Dictionary(grouping: fish, by: { $0 }).mapValues { $0.count }
var fishCount = Dictionary(grouping: fish, by: { $0 }).mapValues { $0.count }
for _ in 1...numDays {
var d = [Int:Int]()
for f in tmpFish.keys.filter({$0 >= 0}) {
d[f-1] = tmpFish[f]
}
let countPregnant = d[-1] ?? 0
d[8] = countPregnant
d[6] = (d[6] ?? 0) + countPregnant
d[-1] = 0
var tmpFishCount = fishCount
.filter { $0.key >= 0 }
.reduce(into: [:]) { $0[$1.key-1, default: 0] = $1.value }
let countPregnant = tmpFishCount[-1] ?? 0
tmpFishCount[8] = countPregnant
tmpFishCount[6] = (tmpFishCount[6] ?? 0) + countPregnant
tmpFishCount[-1] = 0
tmpFish = d
fishCount = tmpFishCount
}
print(tmpFish.values.reduce(0, +))
print(fishCount.values.reduce(0, +))
}
}