Skip to content

Commit

Permalink
feat(js): add solutions for 2024 day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 6, 2024
1 parent 4186b85 commit 757d0c2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 15 deletions.
18 changes: 3 additions & 15 deletions js/src/2024/04/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { Grid, solution } from '../../utils/index.js';
import { ALL_DIRECTIONS, INTERCARDINAL_DIRECTIONS, Grid, solution } from '../../utils/index.js';

const parse = (input) => Grid.from(input);

const DIRECTIONS = [
[-1, -1],
[0, -1],
[1, -1],
[-1, 0],
[1, 0],
[-1, 1],
[0, 1],
[1, 1],
];

// Keep diagonals only
const CROSS_PATTERN_DIRECTIONS = DIRECTIONS.filter(([dx, dy]) => dx !== 0 && dy !== 0);
const CROSS_PATTERN_DIRECTIONS = INTERCARDINAL_DIRECTIONS;

// Whether given letters are all encountered when walking the grid from starting point in the dx/dy direction
const matches = (letters, { grid, point, dx, dy }) =>
Expand All @@ -27,7 +15,7 @@ const search = (grid, word) => {
const letters = word.split('');
let count = 0;
for (const point of grid) {
for (const [dx, dy] of DIRECTIONS) {
for (const [dx, dy] of ALL_DIRECTIONS) {
count += +matches(letters, { grid, point, dx, dy });
}
}
Expand Down
72 changes: 72 additions & 0 deletions js/src/2024/06/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Grid, Orientation, Rotation, solution, dx, dy, normalizeOrientation } from '../../utils/index.js';

const GUARD = '^';
const OBSTACLE = '#';

const parse = (input) => {
const grid = Grid.from(input);
const start = grid.find((point) => point.value === GUARD);
return { grid, start };
};

const explore = (grid, start) => {
let orientation = Orientation.UP;

// Holds points visited by the guard
const visited = new Set();

// Holds points the guard faced (in front)
const faced = new Set();

// Used to track whether the guard patrols in a loop
const cyclic = new Set();
const hash = (point, direction) => `${point.x}:${point.y}:${direction}`;

let current = start;
let isCyclic = false;
while (current) {
const entry = hash(current, orientation);
if (cyclic.has(entry)) {
isCyclic = true;
break;
}
cyclic.add(entry);
visited.add(current);

const next = grid.getPoint(current.x + dx(orientation), current.y + dy(orientation));
if (next) {
faced.add(next);
}
if (next?.value === OBSTACLE) {
orientation = normalizeOrientation(orientation + Rotation.TURN_RIGHT);
} else {
current = next;
}
}
return { visited: Array.from(visited), faced, isCyclic };
};

export const partOne = solution((input) => {
const { grid, start } = parse(input);
return explore(grid, start).visited.length;
});

export const partTwo = solution.inefficient((input) => {
const { grid, start } = parse(input);

const initial = explore(grid, start);

let count = 0;
for (const candidate of initial.faced) {
const original = candidate.value;
candidate.value = OBSTACLE;

const result = explore(grid, start);
if (result.isCyclic) {
count++;
}

candidate.value = original;
}
return count;
});
16 changes: 16 additions & 0 deletions js/src/utils/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { TAU } from './math.js';

const EPSILON = 0.000000001;

export const CARDINAL_DIRECTIONS = [
[0, -1],
[-1, 0],
[1, 0],
[0, 1],
];

export const INTERCARDINAL_DIRECTIONS = [
[-1, -1],
[1, -1],
[-1, 1],
[1, 1],
];

export const ALL_DIRECTIONS = CARDINAL_DIRECTIONS.concat(INTERCARDINAL_DIRECTIONS);

export const Orientation = {
// UP / DOWN are flipped to ensure proper vertical movements in grids
UP: Math.PI * 1.5,
Expand Down
17 changes: 17 additions & 0 deletions puzzles/2024/06/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
part-one:
- input: &example1 |
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
answer: 41

part-two:
- input: *example1
answer: 6

0 comments on commit 757d0c2

Please sign in to comment.