Skip to content

Commit 4793c48

Browse files
committed
day8
1 parent 6f6914c commit 4793c48

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Below is my current advancement in Advent of code:
1515
| Day 5 | :star: (22min) | :star: (21min) |
1616
| Day 6 | :star: | :sad: |
1717
| Day 7 | :star: (45min) | :star: (33min) |
18-
| Day 8 | | |
18+
| Day 8 | :star: (20min) | :star: (16min) |
1919
| Day 9 | | |
2020
| Day 10 | | |
2121
| Day 11 | | |

Sources/AdventOfCode.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let allChallenges: [any AdventDay] = [
99
Day05(),
1010
Day06(),
1111
Day07(),
12+
Day08(),
1213
]
1314

1415
@main

Sources/Day08.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Algorithms
2+
3+
struct Day08: AdventDay {
4+
var antennaLocations: [Character: [Coord]] = [Character: [Coord]]()
5+
var width: Int
6+
var height: Int
7+
8+
init(data: String) {
9+
let lines = data.lines()
10+
height = lines.count
11+
width = lines[0].count
12+
for (colIdx, line) in lines.enumerated() {
13+
for (rowIdx, char) in line.enumerated() {
14+
if char != "." {
15+
let currentPos = Coord(x: rowIdx, y: colIdx)
16+
antennaLocations[char, default: [Coord]()].append(currentPos)
17+
}
18+
}
19+
}
20+
}
21+
22+
func isInside(_ node: Coord) -> Bool {
23+
node.x >= 0 && node.x <= self.width - 1 && node.y >= 0 && node.y <= self.height - 1
24+
}
25+
26+
func part1() -> Int {
27+
var antiNodes: Set<Coord> = Set()
28+
for (key, values) in antennaLocations {
29+
for pair in values.combinations(ofCount: 2) {
30+
let offset = pair[1] - pair[0]
31+
[
32+
pair[0] - offset,
33+
pair[1] + offset,
34+
].filter { isInside($0) }.forEach {
35+
antiNodes.insert($0)
36+
}
37+
}
38+
}
39+
return antiNodes.count
40+
}
41+
42+
func walkAntiNodes(antenna: Coord, offset: Coord) -> [Coord] {
43+
var allAntiNode = [Coord]()
44+
var nodePos = antenna
45+
repeat {
46+
allAntiNode.append(nodePos)
47+
nodePos += offset
48+
} while isInside(nodePos)
49+
return allAntiNode
50+
}
51+
52+
func part2() -> Int {
53+
var antiNodes: Set<Coord> = Set()
54+
for (key, values) in antennaLocations {
55+
for pair in values.combinations(ofCount: 2) {
56+
let offset = pair[1] - pair[0]
57+
var localNodes = walkAntiNodes(antenna: pair[0], offset: offset * -1)
58+
localNodes.append(contentsOf: walkAntiNodes(antenna: pair[1], offset: offset))
59+
localNodes.forEach {
60+
antiNodes.insert($0)
61+
}
62+
}
63+
}
64+
return antiNodes.count
65+
}
66+
}

Sources/Helpers/Coord.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ extension Coord: AdditiveArithmetic {
8080
}
8181
}
8282

83+
extension Coord: DurationProtocol {
84+
static func / (lhs: Coord, rhs: Int) -> Coord {
85+
return Self(x: lhs.x / rhs, y: lhs.y / rhs)
86+
}
87+
88+
static func * (lhs: Coord, rhs: Int) -> Coord {
89+
return Self(x: lhs.x * rhs, y: lhs.y * rhs)
90+
}
91+
92+
static func / (lhs: Coord, rhs: Coord) -> Double {
93+
return 0
94+
}
95+
96+
static func < (lhs: Coord, rhs: Coord) -> Bool {
97+
return false
98+
}
99+
100+
}
101+
83102
extension Array where Array.Element == Coord {
84103
func included(in grid: Grid) -> Self {
85104
self.filter { $0.isInside(grid: grid) }

Tests/Day08.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Testing
2+
3+
@testable import aoc
4+
5+
@Suite("Day08")
6+
struct Day08Tests {
7+
let testData = """
8+
............
9+
........0...
10+
.....0......
11+
.......0....
12+
....0.......
13+
......A.....
14+
............
15+
............
16+
........A...
17+
.........A..
18+
............
19+
............
20+
"""
21+
22+
@Test("part1")
23+
func testPart1() async throws {
24+
let challenge = Day08(data: testData)
25+
#expect(challenge.part1() == 14)
26+
}
27+
28+
@Test("simple part 2")
29+
func testSimplePart2() async throws {
30+
let challenge = Day08(
31+
data: """
32+
T.........
33+
...T......
34+
.T........
35+
..........
36+
..........
37+
..........
38+
..........
39+
..........
40+
..........
41+
..........
42+
""")
43+
#expect(challenge.part2() == 9)
44+
}
45+
46+
@Test("part2")
47+
func testPart2() async throws {
48+
let challenge = Day08(data: testData)
49+
#expect(challenge.part2() == 34)
50+
}
51+
}

0 commit comments

Comments
 (0)