AoC/day6.swift

37 lines
1.3 KiB
Swift
Raw Normal View History

2021-12-12 11:21:58 +01:00
@main
struct Day6 {
let input = [3,3,2,1,4,1,1,2,3,1,1,2,1,2,1,1,1,1,1,1,4,1,1,5,2,1,1,2,1,1,1,3,5,1,5,5,1,1,1,1,3,1,1,3,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,4,1,3,3,1,1,3,1,3,1,2,1,3,1,1,4,1,2,4,4,5,1,1,1,1,1,1,4,1,5,1,1,5,1,1,3,3,1,3,2,5,2,4,1,4,1,2,4,5,1,1,5,1,1,1,4,1,1,5,2,1,1,5,1,1,1,5,1,1,1,1,1,3,1,5,3,2,1,1,2,2,1,2,1,1,5,1,1,4,5,1,4,3,1,1,1,1,1,1,5,1,1,1,5,2,1,1,1,5,1,1,1,4,4,2,1,1,1,1,1,1,1,3,1,1,4,4,1,4,1,1,5,3,1,1,1,5,2,2,4,2,1,1,3,1,5,5,1,1,1,4,1,5,1,1,1,4,3,3,3,1,3,1,5,1,4,2,1,1,5,1,1,1,5,5,1,1,2,1,1,1,3,1,1,1,2,3,1,2,2,3,1,3,1,1,4,1,1,2,1,1,1,1,3,5,1,1,2,1,1,1,4,1,1,1,1,1,2,4,1,1,5,3,1,1,1,2,2,2,1,5,1,3,5,3,1,1,4,1,1,4]
var population: [Int] = .init(repeating: 0, count: 9)
func step() -> [Int] {
var result = Array(repeating: 0, count: 9)
for (age, count) in population.enumerated() {
if age == 0 {
result[6] += count
result[8] += count
} else {
result[age - 1] += count
}
}
return result
}
mutating func run() {
for age in input {
population[age] += 1
}
for day in 0..<256 {
population = step()
}
print(population.reduce(0) { $0 + $1 })
}
static func main() {
var x = Self()
x.run()
}
}