File tree 2 files changed +28
-6
lines changed
aoc22.playground/Pages/Day24.xcplaygroundpage
2 files changed +28
-6
lines changed Original file line number Diff line number Diff line change 3
3
import Foundation
4
4
5
5
part1 ( )
6
+ part2 ( )
6
7
7
8
//: [Next](@next)
Original file line number Diff line number Diff line change @@ -129,13 +129,16 @@ func inputData() -> World {
129
129
)
130
130
}
131
131
132
- func bfs( world: World ) -> Int {
132
+ func bfs( world: World , traversals: Int ) -> Int {
133
+ var goal = world. goal
134
+ var start = world. start
133
135
var canvas = world
134
136
var queue = [ Point] ( )
135
- queue. append ( world . start)
137
+ queue. append ( start)
136
138
var nextQueue = Set < Point > ( )
137
139
var t = - 1
138
-
140
+ var traversal = 1
141
+
139
142
while !( queue. isEmpty && nextQueue. isEmpty) {
140
143
if queue. isEmpty {
141
144
queue = Array ( nextQueue. sorted ( by: { world. goal. manhattan ( $0) < world. goal. manhattan ( $1) } ) . prefix ( 2000 ) )
@@ -144,8 +147,23 @@ func bfs(world: World) -> Int {
144
147
t += 1
145
148
}
146
149
let cur = queue. remove ( at: 0 )
147
- if world. goal == cur {
148
- return t
150
+ if goal == cur {
151
+ if traversal == traversals {
152
+ return t
153
+ }
154
+ if 0 == traversal % 2 {
155
+ goal = world. goal
156
+ start = world. start
157
+ } else {
158
+ goal = world. start
159
+ start = world. goal
160
+ }
161
+ traversal += 1
162
+ queue = [ ]
163
+ queue. append ( start)
164
+ nextQueue = [ ]
165
+ canvas. step ( )
166
+ t += 1
149
167
}
150
168
for n in cur. neighbors ( ) where canvas. contains ( n) {
151
169
nextQueue. insert ( n)
@@ -155,6 +173,9 @@ func bfs(world: World) -> Int {
155
173
}
156
174
157
175
public func part1( ) -> Int {
158
- bfs ( world: inputData ( ) )
176
+ bfs ( world: inputData ( ) , traversals : 1 )
159
177
}
160
178
179
+ public func part2( ) -> Int {
180
+ bfs ( world: inputData ( ) , traversals: 3 )
181
+ }
You can’t perform that action at this time.
0 commit comments