-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.ts
79 lines (68 loc) · 1.65 KB
/
day8.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
import { Point, PointSet, Rect } from './lib/rect.ts';
import { Sequence } from './lib/sequence.ts';
import { type MainArgs, parseFile } from './lib/utils.ts';
type Parsed = string[][];
class Field extends Rect {
antennae = new Map<string, Point[]>();
nodes = new PointSet();
constructor(inp: Parsed, self = false) {
super(inp);
this.forEach((v, x, y) => {
if (v === '.') {
return;
}
let m = this.antennae.get(v);
if (!m) {
m = [];
this.antennae.set(v, m);
}
const p = new Point(x, y);
m.push(p);
if (self) {
this.nodes.add(p);
}
});
}
push(p: Point): boolean {
if (this.check(p)) {
this.nodes.add(p);
return true;
}
return false;
}
get count(): number {
return this.nodes.size;
}
}
function part1(inp: Parsed): number {
const r = new Field(inp);
for (const [_k, v] of r.antennae) {
for (const [a, b] of new Sequence(v).combinations(2)) {
const [dx, dy] = a.delta(b);
r.push(a.xlate(dx, dy));
r.push(b.xlate(-dx, -dy));
}
}
return r.count;
}
function part2(inp: Parsed): number {
const r = new Field(inp, true);
for (const [_k, v] of r.antennae) {
for (const [a, b] of new Sequence(v).combinations(2)) {
const [dx, dy] = a.delta(b);
let p = a;
do {
p = p.xlate(dx, dy);
} while (r.push(p));
p = b;
do {
p = p.xlate(-dx, -dy);
} while (r.push(p));
}
}
return r.count;
}
export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
return [part1(inp), part2(inp)];
}