From b023d0ea192fe6c7d84c32a71c0c3d68767f7205 Mon Sep 17 00:00:00 2001 From: Dory Date: Fri, 13 Dec 2024 15:04:22 -0800 Subject: [PATCH] d08 --- day08/d08p1.swift | 42 ++++++++++++++++++++++++++++++++++++ day08/d08p2.swift | 55 +++++++++++++++++++++++++++++++++++++++++++++++ day08/input.txt | 50 ++++++++++++++++++++++++++++++++++++++++++ day08/test.txt | 12 +++++++++++ 4 files changed, 159 insertions(+) create mode 100644 day08/d08p1.swift create mode 100644 day08/d08p2.swift create mode 100644 day08/input.txt create mode 100644 day08/test.txt diff --git a/day08/d08p1.swift b/day08/d08p1.swift new file mode 100644 index 0000000..f3a02b8 --- /dev/null +++ b/day08/d08p1.swift @@ -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 { + var nodes: Set = [] + for i in 0..= 0 && p.y >= 0 && p.x < height && p.y < width } + .count +print(answer) diff --git a/day08/d08p2.swift b/day08/d08p2.swift new file mode 100644 index 0000000..3a1f765 --- /dev/null +++ b/day08/d08p2.swift @@ -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 { + var nodes: Set = [] + for i in 0..= 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) diff --git a/day08/input.txt b/day08/input.txt new file mode 100644 index 0000000..7a2f532 --- /dev/null +++ b/day08/input.txt @@ -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... diff --git a/day08/test.txt b/day08/test.txt new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/day08/test.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............