diff --git a/Sources/06/06.swift b/Sources/06/06.swift index 3ad671c..fdc996c 100644 --- a/Sources/06/06.swift +++ b/Sources/06/06.swift @@ -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, +)) } }