This commit is contained in:
Max Nuding 2021-12-06 20:23:00 +01:00
parent 1e865820a0
commit 85cfa9003d
Signed by: phlaym
GPG Key ID: A06651BAB6777237
2 changed files with 18 additions and 25 deletions

View File

@ -8,25 +8,6 @@
import Foundation import Foundation
import Runner import Runner
class Fish: CustomStringConvertible {
var timer: Int
var description: String { "\(timer)"}
init(timer: Int = 8) {
self.timer = timer
}
func tick() -> Fish? {
timer -= 1
if timer < 0 {
timer = 6
return Fish()
}
return nil
}
}
struct Day06: Runnable { struct Day06: Runnable {
let inputPath: String let inputPath: String
@ -35,15 +16,26 @@ struct Day06: Runnable {
let internalCounters = input let internalCounters = input
.trimmingCharacters(in: .newlines) .trimmingCharacters(in: .newlines)
.components(separatedBy: ",") .components(separatedBy: ",")
.map { Fish(timer: Int($0)!) } .map { Int($0)! }
run(fish: internalCounters, numDays: 80) run(fish: internalCounters, numDays: 80)
run(fish: internalCounters, numDays: 256)
} }
func run(fish: [Fish], numDays: Int) { func run(fish: [Int], numDays: Int) {
var tmpFish = fish var tmpFish = Dictionary(grouping: fish, by: { $0 }).mapValues { $0.count }
for _ in 1...numDays {
tmpFish.append(contentsOf: tmpFish.compactMap { $0.tick() }) 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
tmpFish = d
} }
print(tmpFish.count) print(tmpFish.values.reduce(0, +))
} }
} }

View File

@ -0,0 +1 @@
3,4,3,1,2