-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay18.swift
More file actions
127 lines (99 loc) · 3.4 KB
/
Copy pathDay18.swift
File metadata and controls
127 lines (99 loc) · 3.4 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
import Algorithms
import AoCCommon
import Foundation
import GameplayKit
typealias GridGraph = GKGridGraph<GKGridGraphNode>
typealias Node = GKGridGraphNode
struct Day18: AdventDay, Sendable {
let data: String
let day = 18
let puzzleName: String = "--- Day 18: RAM Run ---"
init(data: String) {
self.data = data
}
var points: [(Int, Int)] {
do {
return try NumberPairs().parse(data)
} catch {
fatalError("Unable to parse input \(error)")
}
}
func part1() async throws -> String {
let graph = createGraph(width: 71, height: 71)
let points = points.prefix(1024)
remove(points: points, from: graph)
let start = graph.node(atGridPosition: vector_int2(0, 0))!
let end = graph.node(atGridPosition: vector_int2(70, 70))!
let path = graph.findPath(from: start, to: end)
return "\(path.count - 1)" // steps, is one less than length of path
}
func part2() async throws -> String {
obstacle()
// binarySearch()
// bruteForce()J
}
}
extension Day18 {
func createGraph(width: Int, height: Int) -> GridGraph {
GKGridGraph(
fromGridStartingAt: vector_int2(0, 0),
width: Int32(width),
height: Int32(height),
diagonalsAllowed: false,
nodeClass: Node.self
)
}
func nodes(for points: [(Int, Int)].SubSequence, from graph: GridGraph) -> [Node] {
points.map { vector_int2(Int32($0.1), Int32($0.0)) }
.compactMap { graph.node(atGridPosition: $0) }
}
func remove(points: [(Int, Int)].SubSequence, from graph: GridGraph) {
graph.remove(nodes(for: points, from: graph))
}
func bruteForce() -> String {
let graph = createGraph(width: 71, height: 71)
let start = graph.node(atGridPosition: vector_int2(0, 0))!
let end = graph.node(atGridPosition: vector_int2(70, 70))!
remove(points: points.prefix(1024), from: graph)
for point in points.dropFirst(1024) {
let node = graph.node(atGridPosition: vector_int2(Int32(point.1), Int32(point.0)))!
graph.remove([node])
if graph.findPath(from: start, to: end).isEmpty {
return "\(point.0),\(point.1)"
}
}
return "Answer not found"
}
func binarySearch() -> String {
let index = points.partitioningIndex { point in
let graph = createGraph(width: 71, height: 71)
let start = graph.node(atGridPosition: vector_int2(0, 0))!
let end = graph.node(atGridPosition: vector_int2(70, 70))!
let searchIndex = points.firstIndex { $0 == point }!
let slice = points.prefix(through: searchIndex)
remove(points: slice, from: graph)
return graph.findPath(from: start, to: end).isEmpty
}
let point = points[index]
return "\(point.0),\(point.1)"
}
func obstacle() -> String {
let graph = createGraph(width: 71, height: 71)
let start = graph.node(atGridPosition: vector_int2(0, 0))!
let end = graph.node(atGridPosition: vector_int2(70, 70))!
remove(points: points.prefix(1024), from: graph)
var path = graph.findPath(from: start, to: end)
for point in points.dropFirst(1024) {
let node = graph.node(atGridPosition: vector_int2(Int32(point.1), Int32(point.0)))!
graph.remove([node])
guard path.contains(node) else { continue }
let newPath = graph.findPath(from: start, to: end)
if newPath.isEmpty {
return "\(point.0),\(point.1)"
} else {
path = newPath
}
}
return "Answer not found"
}
}