Skip to content

Commit f36e47e

Browse files
committed
Solved 2023 day 10 in Swift
1 parent 5c340a7 commit f36e47e

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@
610610
],
611611
"args": [
612612
"--input",
613-
"input4.txt"
613+
"input.txt"
614614
],
615615
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\10",
616616
"name": "Debug day10b (2023\\swift\\10)",

2023/swift/10/Sources/b/main.swift

+41-42
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ struct Day10: ParsableCommand {
160160
var counterclockwise = 0
161161
var prevDirection = exits[0]
162162
var starters = [Coord: [Direction]]()
163+
var mainLoop = Set<Coord>()
164+
mainLoop.insert(Coord(start))
163165
repeat {
164166
current = add(current, exits[0].coordDiff())
165-
if current == start {
167+
if !mainLoop.insert(Coord(current)).inserted {
166168
break
167169
}
168170
exits[0] = map[current.1][current.0].directions.first(where: { $0 != exits[0].opposite() })!
@@ -186,9 +188,6 @@ struct Day10: ParsableCommand {
186188
counterclockwise += 1
187189
}
188190
}
189-
// if startExits[1] != startExits[0] {
190-
// starters[Coord(start)] = startExits
191-
// }
192191

193192
print("Clockwise is \(clockwise)")
194193
print("Counterclockwise is \(counterclockwise)")
@@ -207,7 +206,7 @@ struct Day10: ParsableCommand {
207206
if c.0 < 0 || c.0 >= map[0].count || c.1 < 0 || c.1 >= map.count {
208207
continue
209208
}
210-
if map[c.1][c.0].type == .ground {
209+
if !mainLoop.contains(Coord(c)) {
211210
trueStarters.append((Coord(c), d))
212211
}
213212
}
@@ -223,47 +222,47 @@ struct Day10: ParsableCommand {
223222
let next = add((current.x, current.y), currentDirection.coordDiff())
224223
if next.1 >= 0 && next.1 < map.count
225224
&& next.0 >= 0 && next.0 < map[0].count
226-
&& map[next.1][next.0].type == .ground {
225+
&& !mainLoop.contains(Coord(next)) {
227226
trueStarters.append((Coord(next), currentDirection))
228227
}
229228
}
230229
print(result)
231230

232-
for (y, line) in map.enumerated() {
233-
for (x, cell) in line.enumerated() {
234-
if (seen.contains(Coord((x, y)))) {
235-
print("O", terminator: "")
236-
continue
237-
}
238-
let d = starters[Coord((x, y))]
239-
if d != nil {
240-
if (d!.count == 2) {
241-
print("x", terminator: "")
242-
} else {
243-
let d2 = d![0].perpendicular()[direction.rawValue]
244-
switch d2 {
245-
case .north:
246-
print("^", terminator: "")
247-
case .south:
248-
print("v", terminator: "")
249-
case .west:
250-
print("<", terminator: "")
251-
case .east:
252-
print(">", terminator: "")
253-
}
254-
}
255-
continue
256-
}
257-
switch cell.type {
258-
case .ground:
259-
print(".", terminator: "")
260-
case .pipe:
261-
print("|", terminator: "")
262-
case .start:
263-
print("S", terminator: "")
264-
}
265-
}
266-
print("")
267-
}
231+
// for (y, line) in map.enumerated() {
232+
// for (x, cell) in line.enumerated() {
233+
// if (seen.contains(Coord((x, y)))) {
234+
// print("O", terminator: "")
235+
// continue
236+
// }
237+
// let d = starters[Coord((x, y))]
238+
// if d != nil {
239+
// if (d!.count == 2) {
240+
// print("x", terminator: "")
241+
// } else {
242+
// let d2 = d![0].perpendicular()[direction.rawValue]
243+
// switch d2 {
244+
// case .north:
245+
// print("^", terminator: "")
246+
// case .south:
247+
// print("v", terminator: "")
248+
// case .west:
249+
// print("<", terminator: "")
250+
// case .east:
251+
// print(">", terminator: "")
252+
// }
253+
// }
254+
// continue
255+
// }
256+
// switch cell.type {
257+
// case .ground:
258+
// print(".", terminator: "")
259+
// case .pipe:
260+
// print("|", terminator: "")
261+
// case .start:
262+
// print("S", terminator: "")
263+
// }
264+
// }
265+
// print("")
266+
// }
268267
}
269268
}

0 commit comments

Comments
 (0)