From cd30cd98fa1a13fb2573550e54b352462ccf87df Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sat, 7 Dec 2024 22:38:53 -0700 Subject: [PATCH] Day 8 --- day8.peggy | 1 + day8.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ inputs | 2 +- t | 9 ------ test/day8.js | 1 + 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 day8.peggy create mode 100644 day8.ts delete mode 100644 t create mode 100644 test/day8.js diff --git a/day8.peggy b/day8.peggy new file mode 100644 index 0000000..cdcfc7c --- /dev/null +++ b/day8.peggy @@ -0,0 +1 @@ +lines = (@[^\n]+ "\n")* diff --git a/day8.ts b/day8.ts new file mode 100644 index 0000000..c7fa79d --- /dev/null +++ b/day8.ts @@ -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(); + 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(); + 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(args); + return [part1(inp), part2(inp)]; +} diff --git a/inputs b/inputs index 977e7c4..7f179a5 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit 977e7c4946d3690df71d455017d993575d975aa8 +Subproject commit 7f179a501ebc3cba464c14c60f099ce151cf0089 diff --git a/t b/t deleted file mode 100644 index fc6e099..0000000 --- a/t +++ /dev/null @@ -1,9 +0,0 @@ -190: 10 19 -3267: 81 40 27 -83: 17 5 -156: 15 6 -7290: 6 8 6 15 -161011: 16 10 13 -192: 17 8 14 -21037: 9 7 18 13 -292: 11 6 16 20 diff --git a/test/day8.js b/test/day8.js new file mode 100644 index 0000000..d5f003b --- /dev/null +++ b/test/day8.js @@ -0,0 +1 @@ +export default [240, 955];