Skip to content

Commit

Permalink
Day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 7, 2024
1 parent 276ef4f commit 642532a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions day6.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lines = (@[^\n]+ "\n")*
78 changes: 78 additions & 0 deletions day6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Dir, Point, Rect } from './lib/rect.ts';
import { type MainArgs, parseFile } from './lib/utils.ts';

type Parsed = string[][];

function path(r: Rect): [Set<number>, Point] {
const [start] = r.filter(val => val === '^');
let pos = start;
let dir = Dir.N;
const visited = new Set<number>();
while (true) {
visited.add(pos.toNumber());
const ahead = pos.inDir(dir);
if (!r.check(ahead)) {
break;
}
const char = r.get(ahead);
if (char === '.' || char === '^') {
pos = ahead;
} else {
dir = (dir + 1) % 4
}
}
return [visited, start];
}

function path2(r: Rect): boolean {
const [start] = r.filter(val => val === '^');
let pos = start;
let dir = Dir.N;
const visitedWithDir = new Set<string>();
while (true) {
const pwd = `${pos},${dir}`;
if (visitedWithDir.has(pwd)) {
return true;
}
visitedWithDir.add(pwd);
const ahead = pos.inDir(dir);
if (!r.check(ahead)) {
return false;
}
const char = r.get(ahead);
if (char === '.' || char === '^') {
pos = ahead;
} else {
dir = (dir + 1) % 4
}
}
}

function part1(inp: Parsed): number {
const r = new Rect(inp);
const [visited] = path(r)
return visited.size;
}

function part2(inp: Parsed): number {
const r = new Rect(inp);
const [visited, start] = path(r)
const points = [...visited].map(v => Point.fromNumber(v))
let tot = 0;
for (const p of points) {
if (p.equals(start)) {
continue;
}
r.set(p, '#');
if (path2(r)) {
tot++;
}
r.set(p, '.');
}
return tot;
}

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
16 changes: 16 additions & 0 deletions lib/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ export class Point implements PointLike {
return `${this.x},${this.y}`;
}

toNumber(size = 16): number {
return (this.x << size) | this.y;
}

static fromNumber(num: number, size = 16): Point {
return new Point(num >> size, num & ((1 << size) - 1));
}

static fromString(str: string): Point {
const m = str.match(/(\d+),(\d+)/);
if (!m) {
throw new Error(`Invalid format: "${str}"`);
}
return new Point(Number(m[1]), Number(m[2]));
}

[Symbol.for('Deno.customInspect')](): string {
return this.toString();
}
Expand Down
1 change: 1 addition & 0 deletions test/day6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [4374, 1705];

0 comments on commit 642532a

Please sign in to comment.