Skip to content

Commit d40f852

Browse files
committed
Swift Day6 part 1
1 parent a7e3a5e commit d40f852

File tree

6 files changed

+322
-11
lines changed

6 files changed

+322
-11
lines changed

2024/Swift/.vscode/launch.json

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"${workspaceFolder:Swift}/problems/day1-1.txt"
1111
],
1212
"cwd": "${workspaceFolder:Swift}",
13-
"name": "Debug aoc2024 day1 part1",
13+
"name": "Day1 part1",
1414
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
1515
"preLaunchTask": "swift: Build Debug aoc2024"
1616
},
@@ -24,7 +24,7 @@
2424
"${workspaceFolder:Swift}/problems/day1-1.txt"
2525
],
2626
"cwd": "${workspaceFolder:Swift}",
27-
"name": "Debug aoc2024 day1 part2",
27+
"name": "Day1 part2",
2828
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
2929
"preLaunchTask": "swift: Build Debug aoc2024"
3030
},
@@ -38,7 +38,7 @@
3838
"${workspaceFolder:Swift}/problems/day2-1.txt"
3939
],
4040
"cwd": "${workspaceFolder:Swift}",
41-
"name": "Debug aocSwift day2 part1",
41+
"name": "Day2 part1",
4242
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
4343
"preLaunchTask": "swift: Build Debug aoc2024"
4444
},
@@ -52,7 +52,7 @@
5252
"${workspaceFolder:Swift}/problems/day2-1.txt"
5353
],
5454
"cwd": "${workspaceFolder:Swift}",
55-
"name": "Debug aoc2024 day2 part2",
55+
"name": "Day2 part2",
5656
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
5757
"preLaunchTask": "swift: Build Debug aoc2024"
5858
},
@@ -66,7 +66,7 @@
6666
"${workspaceFolder:Swift}/problems/day2-1-sample.txt"
6767
],
6868
"cwd": "${workspaceFolder:Swift}",
69-
"name": "Debug sample aoc2024 day2 part2",
69+
"name": "Day2 part2 (sample)",
7070
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
7171
"preLaunchTask": "swift: Build Debug aoc2024"
7272
},
@@ -80,7 +80,7 @@
8080
"${workspaceFolder:Swift}/problems/day3-1.txt"
8181
],
8282
"cwd": "${workspaceFolder:Swift}",
83-
"name": "Debug aoc2024 day3 part1",
83+
"name": "Day3 part1",
8484
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
8585
"preLaunchTask": "swift: Build Debug aoc2024"
8686
},
@@ -94,7 +94,7 @@
9494
"${workspaceFolder:Swift}/problems/day3-1.txt"
9595
],
9696
"cwd": "${workspaceFolder:Swift}",
97-
"name": "Debug aoc2024 day3 part2",
97+
"name": "Day3 part2",
9898
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
9999
"preLaunchTask": "swift: Build Debug aoc2024"
100100
},
@@ -108,7 +108,7 @@
108108
"${workspaceFolder:Swift}/problems/day4-1.txt"
109109
],
110110
"cwd": "${workspaceFolder:Swift}",
111-
"name": "Debug aoc2024 day4 part1",
111+
"name": "Day4 part1",
112112
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
113113
"preLaunchTask": "swift: Build Debug aoc2024"
114114
},
@@ -122,7 +122,7 @@
122122
"${workspaceFolder:Swift}/problems/day4-1.txt"
123123
],
124124
"cwd": "${workspaceFolder:Swift}",
125-
"name": "Debug aoc2024 day4 part2",
125+
"name": "Day4 part2",
126126
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
127127
"preLaunchTask": "swift: Build Debug aoc2024"
128128
},
@@ -136,7 +136,7 @@
136136
"${workspaceFolder:Swift}/problems/day5-1.txt"
137137
],
138138
"cwd": "${workspaceFolder:Swift}",
139-
"name": "Debug aoc2024 day5 part1",
139+
"name": "Day5 part1",
140140
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
141141
"preLaunchTask": "swift: Build Debug aoc2024"
142142
},
@@ -150,7 +150,20 @@
150150
"${workspaceFolder:Swift}/problems/day5-1.txt"
151151
],
152152
"cwd": "${workspaceFolder:Swift}",
153-
"name": "Debug aoc2024 day5 part2",
153+
"name": "Day5 part2",
154+
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
155+
"preLaunchTask": "swift: Build Debug aoc2024"
156+
},{
157+
"type": "lldb",
158+
"request": "launch",
159+
"args": [
160+
"day6",
161+
"part1",
162+
"--input",
163+
"${workspaceFolder:Swift}/problems/day6-1.txt"
164+
],
165+
"cwd": "${workspaceFolder:Swift}",
166+
"name": "Day6 part1",
154167
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
155168
"preLaunchTask": "swift: Build Debug aoc2024"
156169
},

2024/Swift/Sources/aoc2024/AOC2024.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct AOC2024: ParsableCommand {
1111
Day3.self,
1212
Day4.self,
1313
Day5.self,
14+
Day6.self,
1415
]
1516
)
1617
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
struct Day6: ParsableCommand {
5+
static let configuration = CommandConfiguration(
6+
abstract: "Day 6: Guard Gallivant",
7+
subcommands: [Part1.self]
8+
)
9+
10+
enum GuardDirection {
11+
case up
12+
case down
13+
case left
14+
case right
15+
16+
var nextDirection: GuardDirection {
17+
switch self {
18+
case .up: return .right
19+
case .right: return .down
20+
case .down: return .left
21+
case .left: return .up
22+
}
23+
}
24+
}
25+
26+
struct Position: Hashable {
27+
var x: Int
28+
var y: Int
29+
}
30+
31+
enum MovementEnd {
32+
case onBoard([Position])
33+
case offBoard([Position])
34+
}
35+
36+
struct Map {
37+
var guardPosition: Position?
38+
var direction: GuardDirection = .up
39+
let locations: [[Character]]
40+
41+
init(input: String) {
42+
let lines = input.split(separator: "\n")
43+
locations = lines.map { Array($0) }
44+
45+
for lineIndex in 0..<locations.count {
46+
let characters = locations[lineIndex]
47+
for columnIndex in 0..<characters.count {
48+
let character = characters[columnIndex]
49+
if character == "^" {
50+
print("Found guard at \(columnIndex), \(lineIndex)")
51+
guardPosition = Position(x: columnIndex, y: lineIndex)
52+
}
53+
}
54+
}
55+
56+
guard let guardPosition else {
57+
fatalError("Guard position not found on map")
58+
}
59+
60+
guard character(at: guardPosition) == "^" else {
61+
fatalError("Guard position not found on map at \(guardPosition)")
62+
}
63+
}
64+
65+
// Move the guard in the given direction and return the positions it passes through
66+
mutating func moveGuard() -> MovementEnd {
67+
let positions = guardPositions(in: direction)
68+
switch positions {
69+
case .onBoard(let positions):
70+
guardPosition = positions.last
71+
direction = direction.nextDirection
72+
case .offBoard:
73+
guardPosition = nil
74+
}
75+
76+
return positions
77+
}
78+
79+
func guardPositions(in direction: GuardDirection) -> MovementEnd {
80+
var positions: [Position] = []
81+
82+
guard let guardPosition = guardPosition else {
83+
fatalError("Guard position not found on map")
84+
}
85+
86+
var currentPosition = guardPosition
87+
while isOnBoard(position: currentPosition),
88+
character(at: currentPosition) != "#" {
89+
positions.append(currentPosition)
90+
let nextPosition = nextPosition(from: currentPosition, in: direction)
91+
currentPosition = nextPosition
92+
}
93+
94+
if isOnBoard(position: currentPosition) {
95+
return .onBoard(positions)
96+
} else {
97+
return .offBoard(positions)
98+
}
99+
}
100+
101+
private func character(at position: Position) -> Character {
102+
locations[position.y][position.x]
103+
}
104+
105+
private func nextPosition(from position: Position, in direction: GuardDirection) -> Position {
106+
switch direction {
107+
case .up: return Position(x: position.x, y: position.y - 1)
108+
case .down: return Position(x: position.x, y: position.y + 1)
109+
case .left: return Position(x: position.x - 1, y: position.y)
110+
case .right: return Position(x: position.x + 1, y: position.y)
111+
}
112+
}
113+
114+
private func isOnBoard(position: Position) -> Bool {
115+
position.x >= 0 && position.x < locations[0].count && position.y >= 0 && position.y < locations.count
116+
}
117+
}
118+
119+
struct Part1: ParsableCommand {
120+
@OptionGroup var options: CommonOptions
121+
122+
func run() throws {
123+
let input = try String(contentsOf: options.input, encoding: .utf8)
124+
var map = Map(input: input)
125+
var positionsSeen: Set<Position> = []
126+
127+
movementLoop: while true {
128+
let movement = map.moveGuard()
129+
switch movement {
130+
case .onBoard(let positions):
131+
positionsSeen.formUnion(positions)
132+
case .offBoard(let positions):
133+
positionsSeen.formUnion(positions)
134+
break movementLoop
135+
}
136+
}
137+
138+
print(positionsSeen.count)
139+
}
140+
}
141+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

0 commit comments

Comments
 (0)