06b
This commit is contained in:
parent
1e865820a0
commit
85cfa9003d
@ -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 {
|
for _ in 1...numDays {
|
||||||
tmpFish.append(contentsOf: tmpFish.compactMap { $0.tick() })
|
var d = [Int:Int]()
|
||||||
|
for f in tmpFish.keys.filter({$0 >= 0}) {
|
||||||
|
d[f-1] = tmpFish[f]
|
||||||
}
|
}
|
||||||
print(tmpFish.count)
|
let countPregnant = d[-1] ?? 0
|
||||||
|
d[8] = countPregnant
|
||||||
|
d[6] = (d[6] ?? 0) + countPregnant
|
||||||
|
d[-1] = 0
|
||||||
|
|
||||||
|
tmpFish = d
|
||||||
|
}
|
||||||
|
print(tmpFish.values.reduce(0, +))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
Sources/Runner/Resources/input/06_test.txt
Normal file
1
Sources/Runner/Resources/input/06_test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
3,4,3,1,2
|
Loading…
Reference in New Issue
Block a user