-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
106 lines (85 loc) · 2.18 KB
/
index.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
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
102
103
104
105
106
import {
INPUT_PATH,
runner,
SAMPLE_PATH,
splitLines,
} from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const debug = (lines: string[][]) => {
lines.map((e, y) => console.log(y, e.join('')));
console.log('\n');
};
type Antena = {
name: string;
x: number;
y: number;
};
const p1 = (lines: string[][]) => {
debug(lines);
const antenas: Antena[] = [];
lines.forEach((row, idy) => {
row.forEach((cell, idx) => {
if (cell !== '.') antenas.push({ name: cell, x: idx, y: idy });
});
});
antenas.forEach((item, idx) => {
antenas.forEach(({ x, y, name }) => {
if (name === item.name && item.x !== x && item.y !== y) {
const diffY = item.y + (item.y - y);
const diffX = item.x + (item.x - x);
if (lines[diffY]?.[diffX]) {
lines[diffY][diffX] = '#';
}
}
});
});
let counter = 0;
lines.forEach((row) =>
row.forEach((cell, idx) => {
if (cell === '#') counter++;
}),
);
debug(lines);
return counter;
};
const p2 = (lines: string[][]) => {
const antennas: Antena[] = [];
let counter = 0;
const yMax = lines.length;
const xMax = lines[0].length;
lines.forEach((row, idy) => {
row.forEach((cell, idx) => {
if (cell !== '.') antennas.push({ name: cell, x: idx, y: idy });
});
});
antennas.forEach((item, idx) => {
antennas.forEach(({ x, y, name }) => {
if (name === item.name && item.x !== x && item.y !== y) {
let diffY = item.y + (item.y - y);
let diffX = item.x + (item.x - x);
let factor = 1;
while (diffX <= xMax && diffY <= yMax && diffY >= 0 && diffY >= 0) {
if (lines[diffY]?.[diffX] === '.') {
lines[diffY][diffX] = '#';
}
diffY = item.y + (item.y - y) * factor;
diffX = item.x + (item.x - x) * factor++;
}
}
});
});
lines.forEach((row) =>
row.forEach((cell) => {
if (cell === '#') counter++;
}),
);
// debug(lines);
return counter + antennas.length;
};
runner({
path,
solution: (input) => {
const lines = splitLines(input).map((row) => row.split(''));
return { p2: p2(lines) };
},
});