This commit is contained in:
2024-12-13 15:04:22 -08:00
parent 4e11f94a22
commit b023d0ea19
4 changed files with 159 additions and 0 deletions

42
day08/d08p1.swift Normal file
View File

@@ -0,0 +1,42 @@
import Foundation
struct Point : Hashable {
let x: Int
let y: Int
}
func readInput(_ filePath: String) throws -> (Int, Int, [Character: [Point]]) {
let content = try String(contentsOfFile: filePath, encoding: .ascii)
let lines = content.split(separator: "\n").map(Array.init)
let antennas = lines.enumerated().flatMap { i, line in
line.enumerated().compactMap { j, cell in
cell == "." ? nil : (cell, i, j)
}
}
var map: [Character: [Point]] = [:]
for (type, i, j) in antennas {
let current = map[type, default: []]
map[type] = current + [Point(x: i, y: j)]
}
return (lines.count, lines[0].count, map)
}
func nodes(_ antennas: [Point]) -> Set<Point> {
var nodes: Set<Point> = []
for i in 0..<antennas.count {
for j in i+1..<antennas.count {
let (a, b) = (antennas[i], antennas[j])
nodes.insert(Point(x: 2*a.x - b.x, y: 2*a.y - b.y))
nodes.insert(Point(x: 2*b.x - a.x, y: 2*b.y - a.y))
}
}
return nodes
}
let (height, width, antennas) = try readInput(CommandLine.arguments[1])
let answer = antennas
.map { c, points in nodes(points) }
.reduce(Set([]), { acc, x in acc.union(x) })
.filter { p in p.x >= 0 && p.y >= 0 && p.x < height && p.y < width }
.count
print(answer)

55
day08/d08p2.swift Normal file
View File

@@ -0,0 +1,55 @@
import Foundation
struct Point : Hashable {
let x: Int
let y: Int
}
func readInput(_ filePath: String) throws -> (Int, Int, [Character: [Point]]) {
let content = try String(contentsOfFile: filePath, encoding: .ascii)
let lines = content.split(separator: "\n").map(Array.init)
let antennas = lines.enumerated().flatMap { i, line in
line.enumerated().compactMap { j, cell in
cell == "." ? nil : (cell, i, j)
}
}
var map: [Character: [Point]] = [:]
for (type, i, j) in antennas {
let current = map[type, default: []]
map[type] = current + [Point(x: i, y: j)]
}
return (lines.count, lines[0].count, map)
}
func nodes(_ antennas: [Point], h: Int, w: Int) -> Set<Point> {
var nodes: Set<Point> = []
for i in 0..<antennas.count {
for j in i+1..<antennas.count {
let (a, b) = (antennas[i], antennas[j])
// (k+1)a - kb going one way
var k = 0
var next = Point(x: (k+1)*a.x - k*b.x, y: (k+1)*a.y - k*b.y)
while next.x >= 0 && next.x < h && next.y >= 0 && next.y < w {
nodes.insert(next)
k += 1
next = Point(x: (k+1)*a.x - k*b.x, y: (k+1)*a.y - k*b.y)
}
// going the other way
k = -1
next = Point(x: (k+1)*a.x - k*b.x, y: (k+1)*a.y - k*b.y)
while next.x >= 0 && next.x < h && next.y >= 0 && next.y < w {
nodes.insert(next)
k -= 1
next = Point(x: (k+1)*a.x - k*b.x, y: (k+1)*a.y - k*b.y)
}
}
}
return nodes
}
let (height, width, antennas) = try readInput(CommandLine.arguments[1])
let answer = antennas
.map { c, points in nodes(points, h: height, w: width) }
.reduce(Set([]), { acc, x in acc.union(x) })
.count
print(answer)

50
day08/input.txt Normal file
View File

@@ -0,0 +1,50 @@
...d............................J.................
......e.............................J.............
..........6............7..........................
........................P7........................
..................................................
.........6........................................
e..........................x.................E....
...G...A.......d...........................o......
.....A.e...........................J......8.......
................6....9.....J.............E.8......
..........d.9.........7..K....E...................
...e.....U....9................x..K...............
......A......O...........P................o.......
......................x..................M..E.....
........................x........p................
........A..................O......................
.......r.f....O.......P9..G.........m.............
u...df..r...............7.........................
.....g.............nXu......N.........K...........
..............l..........0..............p.........
.......lu...................p......o..............
....g..........l........0p..G.....F...............
.....................................8......F.....
...................................C..............
....3................G0......................M....
2...f....g..........3........P......O......F......
g......3.....0....H......................F..M.....
.............c................m...h.....M.........
...........2....l.................................
..U...c......2...........................K........
.D....................r.....f.....................
....................N.............................
.U..............h.................................
...a.............u..............C.................
c...Uj....a..6...H...................R............
...3....j................H...............m........
.......................5.......C..........4....m..
......................H.........R......N....X.....
.........h..2.................R................N..
.......................r...........q...n..........
.....c..............5.............................
..a..h....D.................................n.....
......qk..................D............1.....X....
.k..................................q.............
..k..........a.............L................1....4
......k..........RQ..5.L.j..1..................4..
..................................................
..............L.....................oX............
........Q.............L.........n.................
...........Q.D........5..........1............4...

12
day08/test.txt Normal file
View File

@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............