06a
This commit is contained in:
49
Sources/06/06.swift
Normal file
49
Sources/06/06.swift
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
}
|
11
Sources/06/main.swift
Normal file
11
Sources/06/main.swift
Normal file
@ -0,0 +1,11 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Max Nuding on 05.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Runner
|
||||
|
||||
Runner(target: Day06.self, day: "06").run()
|
Reference in New Issue
Block a user