Skip to content

Commit 94fdc13

Browse files
committed
Swift Day8 part 1
1 parent bf3f853 commit 94fdc13

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

2024/Swift/.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@
210210
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
211211
"preLaunchTask": "swift: Build Debug aoc2024"
212212
},
213+
{
214+
"type": "lldb",
215+
"request": "launch",
216+
"args": [
217+
"day8",
218+
"part1",
219+
"--input",
220+
"${workspaceFolder:problems}/day8-1.txt"
221+
],
222+
"cwd": "${workspaceFolder:Swift}",
223+
"name": "Day8 part1 (sample1)",
224+
"program": "${workspaceFolder:Swift}/.build/debug/aoc2024",
225+
"preLaunchTask": "swift: Build Debug aoc2024"
226+
},
213227
{
214228
"type": "lldb",
215229
"request": "launch",

2024/Swift/Sources/aoc2024/AOC2024.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct AOC2024: ParsableCommand {
1313
Day5.self,
1414
Day6.self,
1515
Day7.self,
16+
Day8.self,
1617
]
1718
)
1819
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
struct Day8: ParsableCommand {
5+
static let configuration = CommandConfiguration(
6+
abstract: "Day 8: Resonant Collinearity",
7+
subcommands: [Part1.self]
8+
)
9+
10+
struct Position: Hashable, Codable {
11+
let x: Int
12+
let y: Int
13+
}
14+
15+
struct Map: Codable {
16+
let frequencies: [String: [Position]]
17+
let width: Int
18+
let height: Int
19+
20+
init(contents: String) {
21+
let lines = contents.split(separator: "\n")
22+
self.width = lines[0].count
23+
self.height = lines.count
24+
var frequencies: [String: [Position]] = [:]
25+
for (yIndex, line) in lines.enumerated() {
26+
for (xIndex, char) in line.enumerated() where char != "." {
27+
frequencies[String(char), default: []].append(Position(x: xIndex, y: yIndex))
28+
}
29+
}
30+
self.frequencies = frequencies
31+
}
32+
33+
func antinodes() -> [String: Set<Position>] {
34+
var antinodes = [String: Set<Position>]()
35+
for frequency in frequencies.keys {
36+
let frequencyAntinodes = self.antinodes(for: frequency)
37+
let filtered = frequencyAntinodes.filter { $0.x >= 0 && $0.x < width && $0.y >= 0 && $0.y < height }
38+
antinodes[frequency] = Set(filtered)
39+
}
40+
return antinodes
41+
}
42+
43+
func antinodes(for frequency: String) -> [Position] {
44+
let positions = frequencies[frequency]!
45+
var frequencyAntinodes = [Position]()
46+
for firstIndex in 0..<positions.count - 1 {
47+
for secondIndex in firstIndex+1..<positions.count {
48+
let antinodes = antinodes(for: positions[firstIndex], and: positions[secondIndex])
49+
frequencyAntinodes.append(contentsOf: antinodes)
50+
}
51+
}
52+
53+
return frequencyAntinodes
54+
}
55+
56+
func antinodes(for first: Position, and second: Position) -> [Position] {
57+
let (left, right) = first.x <= second.x ? (first, second) : (second, first)
58+
let rise = abs(right.y - left.y)
59+
let run = abs(right.x - left.x)
60+
switch run {
61+
case 0:
62+
fatalError("Vertical line")
63+
break
64+
default:
65+
let leftAntinode: Position
66+
let rightAntinode: Position
67+
if left.y > right.y {
68+
leftAntinode = Position(x: left.x - run, y: left.y + rise)
69+
rightAntinode = Position(x: right.x + run, y: right.y - rise)
70+
} else if left.y < right.y {
71+
leftAntinode = Position(x: left.x - run, y: left.y - rise)
72+
rightAntinode = Position(x: right.x + run, y: right.y + rise)
73+
} else {
74+
leftAntinode = Position(x: left.x - run, y: left.y)
75+
rightAntinode = Position(x: right.x + run, y: right.y)
76+
}
77+
return [leftAntinode, rightAntinode]
78+
}
79+
}
80+
81+
func printMapOfAntinodes(for frequency: String) {
82+
let antinodes = self.antinodes()
83+
let antinodesForFrequency = antinodes[frequency]!
84+
var map = Array(repeating: Array(repeating: ".", count: width), count: height)
85+
for antinode in antinodesForFrequency {
86+
map[antinode.y][antinode.x] = "X"
87+
}
88+
for frequencyPosition in frequencies[frequency]! {
89+
map[frequencyPosition.y][frequencyPosition.x] = frequency
90+
}
91+
print(map.map { $0.joined() }.joined(separator: "\n"))
92+
}
93+
}
94+
95+
struct Part1: ParsableCommand {
96+
@OptionGroup var options: CommonOptions
97+
98+
mutating func run() throws {
99+
let contents = try String(contentsOf: options.input, encoding: .utf8)
100+
let map = Map(contents: contents)
101+
let antinodes = map.antinodes()
102+
print(antinodes.count)
103+
let antinodesSet = antinodes.reduce(into: Set<Position>()) { $0.formUnion($1.value) }
104+
print(antinodesSet.count)
105+
}
106+
}
107+
}

2024/problems/day8-1-sample1.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
..........
2+
..........
3+
..........
4+
....a.....
5+
..........
6+
.....a....
7+
..........
8+
..........
9+
..........
10+
..........
11+

2024/problems/day8-1-sample2.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..........
2+
..........
3+
..........
4+
....a.....
5+
........a.
6+
.....a....
7+
..........
8+
..........
9+
..........
10+
..........

2024/problems/day8-1-sample3.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..........
2+
..........
3+
..........
4+
....a.....
5+
........a.
6+
.....a....
7+
..........
8+
......A...
9+
..........
10+
..........

2024/problems/day8-1-sample4.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............
13+

2024/problems/day8-1.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.AU..Z.....8.......................t..C.6.........
2+
..................................................
3+
.....K.U....................v.....................
4+
...Z..A.............................v8.....t......
5+
p..................a8...........b...t.............
6+
..pU.....A..4.....................................
7+
..........................E.......................
8+
...........K..V..............v8.....Cb............
9+
....V................b...p........................
10+
....7.............................................
11+
....4.....A..........V......K..E.....6............
12+
.4.........................Vb...........0..C......
13+
..................................k........N......
14+
K....7...........9...........6.....kE.............
15+
......7......1...................k.......C........
16+
...p....................9....0.....N6.............
17+
..........Z........e..1...........................
18+
.............................E................N...
19+
...4...............................v0..........z..
20+
........U.....Z......1................z..a........
21+
.....5.......7......................N.............
22+
....................n.............................
23+
.......................0.9...c..........z.d.T.....
24+
...................n.W......a...t......D....d.....
25+
..........I.....e......................o9.........
26+
....5..2................e...........D.............
27+
...........................n......D...............
28+
......25I...1..................c......W.......o...
29+
................n..............D..................
30+
...........I........i..e..........................
31+
......5......2.....P..............a...............
32+
...........................z..................T...
33+
..........j.....................Wd...........O..o.
34+
................................................c.
35+
.................I................B...............
36+
...........u.............................T.d......
37+
.............................................J....
38+
.....3.i....u......................o..............
39+
3...i.............................................
40+
..................................................
41+
...........j...............W....O............w....
42+
...P........................J.....................
43+
.....u............................................
44+
.............................w....................
45+
......u.................2...w...J.................
46+
.....j.....B3......................O..............
47+
P....B..............................c.............
48+
................B.............w...................
49+
.....i.............3..............................
50+
..P.j....................J..........O.............

0 commit comments

Comments
 (0)