-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay20.swift
More file actions
132 lines (115 loc) · 2.96 KB
/
Day20.swift
File metadata and controls
132 lines (115 loc) · 2.96 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Algorithms
import AoCCommon
import Foundation
import Parsing
struct Day20: AdventDay, Sendable {
let data: String
let day = 20
let puzzleName: String = "--- Day 20: Race Condition ---"
init(data: String) {
self.data = data
}
// (dictionary of distance from start, start, end)
var track: [Cell: Int] {
do {
let rows = try InputParser().parse(data)
return track(rows)
} catch {
fatalError("Unable to parse data \(error)")
}
}
func part1() async throws -> Int {
countCheats(track, radius: 2, minReduction: 100)
}
func part2() async throws -> Int {
countCheats(track, radius: 20, minReduction: 100)
}
}
extension Day20 {
func track(_ rows: [[Character]]) -> [Cell: Int] {
let grid = Grid(rows: rows)
let start = grid.firstCell(for: "S")!
let end = grid.firstCell(for: "E")!
var path: [Cell] = []
var queue: Deque<[Cell]> = [[start]]
var seen: Set<Cell> = []
while !queue.isEmpty, path.isEmpty {
let currentPath = queue.removeFirst()
let head = currentPath.last!
if head == end {
path = currentPath
continue
}
if seen.contains(head) {
continue
} else {
seen.insert(head)
}
let neighbours = grid.neighbours(head, includeDiagonals: false)
for neighbour in neighbours {
guard grid.element(neighbour) != "#",
!seen.contains(neighbour)
else {
continue
}
queue.append(currentPath + [neighbour])
}
}
var dict = [Cell: Int]()
for (distance, position) in path.enumerated() {
dict[position] = distance
}
return dict
}
func countCheats(_ track: [Cell: Int], radius: Int, minReduction reduction: Int) -> Int {
let path = track.sorted { $0.value < $1.value }
return path.map { cell, distance in
var candidates: Set<Cell> = []
let targets = targets(from: cell, distance: radius)
for (c, d) in targets {
guard let dd = track[c],
!candidates.contains(c),
dd >= distance + d + reduction
else {
continue
}
candidates.insert(c)
}
return candidates.count
}.reduce(0, +)
}
func targets(from: Cell, distance: Int) -> [(Cell, Int)] {
var cells: [(Cell, Int)] = []
for r in -distance ... distance {
for c in -distance ... distance {
guard abs(r) + abs(c) <= distance
else {
continue
}
cells.append((Cell(from.row + r, from.col + c), abs(r) + abs(c)))
}
}
return cells
}
}
// MARK: - Parsing
extension Day20 {
struct ParseLine: Parser {
var body: some Parser<Substring, [Character]> {
Parse(Array.init) {
Prefix { $0 != "\n" }
}
}
}
struct InputParser: Parser {
var body: some Parser<Substring, [[Character]]> {
Many {
ParseLine()
} separator: {
"\n"
} terminator: {
End()
}
}
}
}