aoc2021/Sources/06/06.swift

50 lines
1012 B
Swift
Raw Normal View History

2021-12-06 18:37:25 +00:00
//
// File.swift
//
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
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 {
let inputPath: String
func run() {
let input = try! String(contentsOfFile: inputPath)
let internalCounters = input
.trimmingCharacters(in: .newlines)
.components(separatedBy: ",")
.map { Fish(timer: Int($0)!) }
run(fish: internalCounters, numDays: 80)
}
func run(fish: [Fish], numDays: Int) {
var tmpFish = fish
for _ in 1...numDays {
tmpFish.append(contentsOf: tmpFish.compactMap { $0.tick() })
}
print(tmpFish.count)
}
}