Skip to content

Commit

Permalink
feat(js): add solutions for 2024 day 20 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 22, 2024
1 parent d7ed943 commit a2b3204
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
51 changes: 51 additions & 0 deletions js/src/2024/20/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Grid, astar, solution } from '../../utils/index.js';

const WALL = '#';

const isPassable = (point) => point.value !== WALL;

const directions = ['up', 'down', 'left', 'right'];

const parse = (input) => {
const track = Grid.from(input);
const start = track.find((point) => point.value === 'S');
const end = track.find((point) => point.value === 'E');
return { track, start, end };
};

const traverse = ({ start, end }) => {
const result = astar(start, null, {
done: (current) => current === end,
neighborsFor: (current) => current.adjacentNeighbors.filter(isPassable),
});
return result;
};

export const partOne = solution.inefficient((input, { minSavings = 100 }) => {
const { start, end } = parse(input);

const honest = traverse({ start, end });
const target = honest.score - minSavings;

let count = 0;
for (const enter of honest.path) {
for (const dir of directions) {
const phase = enter[dir];
if (!phase || isPassable(phase)) continue;

const exit = phase[dir];
if (!exit || !isPassable(exit)) continue;

const first = traverse({ start, end: enter });
const second = traverse({ start: exit, end });
const score = first.score + 2 + second.score;

if (score <= target) {
++count;
}
}
}
return count;
});

// TODO: Ain't nobody got time for part two ;)
20 changes: 20 additions & 0 deletions puzzles/2024/20/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
part-one:
- input: |
###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############
answer: 4
args:
min-savings: 30

0 comments on commit a2b3204

Please sign in to comment.