Skip to content

Commit

Permalink
Day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 8, 2024
1 parent af573b9 commit cd30cd9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
1 change: 1 addition & 0 deletions day8.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lines = (@[^\n]+ "\n")*
87 changes: 87 additions & 0 deletions day8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Point, 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: Point[] = [];

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.push(p);
}
});
}

push(p: Point): boolean {
if (!this.check(p)) {
return false;
}
this.nodes.push(p);
return true;
}

count(): number {
const locs = new Set<string>();
for (const p of this.nodes) {
if (this.check(p)) {
locs.add(p.toString());
}
}
return locs.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 = a.x - b.x;
const dy = a.y - b.y;
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 = a.x - b.x;
const dy = a.y - b.y;
let p = a.xlate(dx, dy);
while (r.push(p)) {
p = p.xlate(dx, dy);
}
p = b.xlate(-dx, -dy);
while (r.push(p)) {
p = p.xlate(-dx, -dy);
}
}
}
return r.count();
}

export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
return [part1(inp), part2(inp)];
}
2 changes: 1 addition & 1 deletion inputs
9 changes: 0 additions & 9 deletions t

This file was deleted.

1 change: 1 addition & 0 deletions test/day8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [240, 955];

0 comments on commit cd30cd9

Please sign in to comment.