-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2021_11.swift
84 lines (71 loc) · 2.19 KB
/
Solution_2021_11.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Solution_2021_11.swift
//
//
// Created by Ivan Chalov on 11.12.21.
//
import Foundation
struct Solution_2021_11: Solution {
var input: Input
func run() throws {
let input = try self.input.get()
.split(whereSeparator: \.isNewline)
.map { $0.map { Int(String($0))! } }
// ------- Part 1 -------
let octopusesSeq = sequence(
state: input,
next: { state -> Int in
incrementAll(&state)
return walkAll(&state)
})
let part1 = octopusesSeq.prefix(100).reduce(0, +)
print(part1)
// ------- Part 2 -------
let octopusesCount = input.count * input.first!.count
let part2 = octopusesSeq
.enumerated()
.first(where: { $0.element == octopusesCount })!
.offset + 1
print(part2)
// ------- Test -------
assert(part1 == 1723, "WA")
assert(part2 == 327, "WA")
}
}
private func incrementAll(_ m: inout [[Int]]) {
for i in m.indices {
for j in m[i].indices {
m[i][j] += 1
}
}
}
private func walkAll(_ m: inout [[Int]]) -> Int {
m.indices
.map { i in
m[i].indices
.map { j in walkOctopuses(i: i, j: j, &m) }
.reduce(0, +)
}
.reduce(0, +)
}
private func bumpAdjacent(i: Int, j: Int, _ m: inout [[Int]]) {
[(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
.forEach { dirI, dirJ in
let (idxI, idxJ) = (i + dirI, j + dirJ)
if m.indices ~= idxI, m[idxI].indices ~= idxJ, m[idxI][idxJ] != 0 {
m[idxI][idxJ] += 1
}
}
}
private func walkOctopuses(i: Int, j: Int, _ m: inout [[Int]]) -> Int {
guard m[i][j] > 9 else { return 0 }
m[i][j] = 0
bumpAdjacent(i: i, j: j, &m)
return 1 + [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
.compactMap { dirI, dirJ in
let (idxI, idxJ) = (i + dirI, j + dirJ)
return m.indices ~= idxI && m[idxI].indices ~= idxJ
? walkOctopuses(i: idxI, j: idxJ, &m)
: nil
}.reduce(0, +)
}