-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.ts
More file actions
101 lines (87 loc) · 2.14 KB
/
day08.ts
File metadata and controls
101 lines (87 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { readInput, gridLocations } from "./util.ts";
type antinodeGenerator = (
grid: string[],
x1: number,
y1: number,
x2: number,
y2: number
) => Generator<number[], void, unknown>;
const allAntinodes: antinodeGenerator = function* (grid, x1, y1, x2, y2) {
const [rise, run] = [y2 - y1, x2 - x1];
while (grid[y1]?.[x1] !== undefined) {
yield [x1, y1];
(x1 -= run), (y1 -= rise);
}
while (grid[y2]?.[x2] !== undefined) {
yield [x2, y2];
(x2 += run), (y2 += rise);
}
};
const closestAntinodes: antinodeGenerator = function* (_grid, x1, y1, x2, y2) {
const [rise, run] = [y2 - y1, x2 - x1];
yield [x1 - run, y1 - rise];
yield [x2 + run, y2 + rise];
};
function countAntinodes(input: string, iterAntinodes: antinodeGenerator) {
const antinodes = new Set<string>();
const grid = input.split("\n");
const antennas = gridLocations(grid);
for (const locations of Object.values(antennas)) {
for (let i = 0; i < locations.length; i++) {
for (let j = 0; j < locations.length; j++) {
if (i === j) continue;
for (const [x, y] of iterAntinodes(
grid,
...locations[i],
...locations[j]
)) {
if (grid[y]?.[x] !== undefined) antinodes.add(`${x},${y}`);
}
}
}
}
return antinodes.size;
}
function part1(input: string): number {
return countAntinodes(input, closestAntinodes);
}
function part2(input: string): number {
return countAntinodes(input, allAntinodes);
}
if (import.meta.main) {
const input = await readInput();
console.log("Part 1:", part1(input));
console.log("Part 2:", part2(input));
}
// Test
import { assertEquals } from "jsr:@std/assert@1.0.8";
Deno.test("Part 1: ", () => {
const out = part1(`............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............`);
assertEquals(out, 14);
});
Deno.test("Part 2: ", () => {
const out = part2(`............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............`);
assertEquals(out, 34);
});