-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2021_06.swift
45 lines (38 loc) · 1.17 KB
/
Solution_2021_06.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Solution_2021_06.swift
//
//
// Created by Ivan Chalov on 06.12.21.
//
import Foundation
struct Solution_2021_06: Solution {
var input: Input
func run() throws {
let input = try self.input.get()
.split(separator: ",")
.map { Int($0)! }
// ------- Part 1 -------
func solve(rounds: Int) -> Int {
(1...rounds)
.reduce(Dictionary(grouping: input, by: { $0 }).mapValues(\.count)) { timers, _ in
var newTimers = [Int: Int]()
for timer in 1...8 {
newTimers[timer - 1] = timers[timer]
}
let newFish = timers[0] ?? 0
newTimers[6, default: 0] += newFish
newTimers[8, default: 0] += newFish
return newTimers
}
.values.reduce(0, +)
}
let part1 = solve(rounds: 80)
print(part1)
// ------- Part 2 -------
let part2 = solve(rounds: 256)
print(part2)
// ------- Test -------
assert(part1 == 361169, "WA")
assert(part2 == 1634946868992, "WA")
}
}