2021-12-06 18:37:25 +00:00
|
|
|
//
|
|
|
|
// File.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Max Nuding on 05.12.21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Runner
|
|
|
|
|
|
|
|
struct Day06: Runnable {
|
|
|
|
let inputPath: String
|
|
|
|
|
|
|
|
func run() {
|
|
|
|
let input = try! String(contentsOfFile: inputPath)
|
|
|
|
let internalCounters = input
|
|
|
|
.trimmingCharacters(in: .newlines)
|
|
|
|
.components(separatedBy: ",")
|
2021-12-06 19:23:00 +00:00
|
|
|
.map { Int($0)! }
|
2021-12-06 18:37:25 +00:00
|
|
|
run(fish: internalCounters, numDays: 80)
|
2021-12-06 19:23:00 +00:00
|
|
|
run(fish: internalCounters, numDays: 256)
|
2021-12-06 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 19:23:00 +00:00
|
|
|
func run(fish: [Int], numDays: Int) {
|
|
|
|
var tmpFish = 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
|
|
|
|
|
|
|
|
tmpFish = d
|
2021-12-06 18:37:25 +00:00
|
|
|
}
|
2021-12-06 19:23:00 +00:00
|
|
|
print(tmpFish.values.reduce(0, +))
|
2021-12-06 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|