diff --git a/Package.swift b/Package.swift index 9731741..04c2e4c 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,8 @@ let package = Package( .executable(name: "02", targets: ["02"]), .executable(name: "03", targets: ["03"]), .executable(name: "04", targets: ["04"]), - .executable(name: "05", targets: ["05"]) + .executable(name: "05", targets: ["05"]), + .executable(name: "06", targets: ["06"]) ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -58,6 +59,10 @@ let package = Package( .targetItem(name: "Runner", condition: nil) ], resources: [] + ), + .executableTarget( + name: "06", + dependencies: [.targetItem(name: "Runner", condition: nil)] ) ] ) diff --git a/Sources/06/06.swift b/Sources/06/06.swift new file mode 100644 index 0000000..8a65fa2 --- /dev/null +++ b/Sources/06/06.swift @@ -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) + } +} diff --git a/Sources/06/main.swift b/Sources/06/main.swift new file mode 100644 index 0000000..0ced939 --- /dev/null +++ b/Sources/06/main.swift @@ -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() diff --git a/Sources/Runner/Resources/input/06.txt b/Sources/Runner/Resources/input/06.txt new file mode 100644 index 0000000..e605103 --- /dev/null +++ b/Sources/Runner/Resources/input/06.txt @@ -0,0 +1 @@ +1,2,1,3,2,1,1,5,1,4,1,2,1,4,3,3,5,1,1,3,5,3,4,5,5,4,3,1,1,4,3,1,5,2,5,2,4,1,1,1,1,1,1,1,4,1,4,4,4,1,4,4,1,4,2,1,1,1,1,3,5,4,3,3,5,4,1,3,1,1,2,1,1,1,4,1,2,5,2,3,1,1,1,2,1,5,1,1,1,4,4,4,1,5,1,2,3,2,2,2,1,1,4,3,1,4,4,2,1,1,5,1,1,1,3,1,2,1,1,1,1,4,5,5,2,3,4,2,1,1,1,2,1,1,5,5,3,5,4,3,1,3,1,1,5,1,1,4,2,1,3,1,1,4,3,1,5,1,1,3,4,2,2,1,1,2,1,1,2,1,3,2,3,1,4,5,1,1,4,3,3,1,1,2,2,1,5,2,1,3,4,5,4,5,5,4,3,1,5,1,1,1,4,4,3,2,5,2,1,4,3,5,1,3,5,1,3,3,1,1,1,2,5,3,1,1,3,1,1,1,2,1,5,1,5,1,3,1,1,5,4,3,3,2,2,1,1,3,4,1,1,1,1,4,1,3,1,5,1,1,3,1,1,1,1,2,2,4,4,4,1,2,5,5,2,2,4,1,1,4,2,1,1,5,1,5,3,5,4,5,3,1,1,1,2,3,1,2,1,1