-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.ts
74 lines (64 loc) · 1.79 KB
/
day08.ts
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
import type { Day } from './Day.ts';
import { enumGrid } from './utils.ts';
export class DayImpl implements Day {
private readonly input: string[][];
constructor(input: string) {
this.input = this.parseInput(input);
}
parseInput(input: string) {
return input
.trim()
.split('\n')
.map((line: string) => {
return line
.split('');
});
}
partOne() {
const antennas = antennasPositions(this.input);
return getUniqAntinodes(this.input, antennas, false);
}
partTwo() {
const antennas = antennasPositions(this.input);
return getUniqAntinodes(this.input, antennas, true);
}
}
function antennasPositions(grid: string[][]) {
const antennas = new Map();
for (const { x, y, cell } of enumGrid(grid)) {
if (cell === '.') {
continue;
}
if (!antennas.has(cell)) {
antennas.set(cell, []);
}
antennas.get(cell).push([x, y]);
}
return antennas;
}
function getUniqAntinodes(grid: string[][], antennas: Map<string, number[][]>, extended: boolean): number {
const antinodes = new Set();
for (const [_, positions] of antennas) {
positions.forEach(([y, x], i) => {
positions
.filter((_, j) => i !== j)
.reduce((acc, [y2, x2]) => {
const [dy, dx] = [y2 - y, x2 - x];
const k = extended ? 1 : 2;
let [py, px] = [y + k * dy, x + k * dx];
if (!extended) {
acc.push([py, px]);
return acc;
}
while (grid[py]?.[px]) {
acc.push([py, px]);
[py, px] = [py + dy, px + dx];
}
return acc;
}, [] as number[][])
.filter(([y, x]) => grid[y]?.[x])
.forEach((position: number[]) => antinodes.add(position.join(',')));
});
}
return antinodes.size;
}