Skip to content

Commit

Permalink
feat(js): add solutions for 2024 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 10, 2024
1 parent 3a6d7c2 commit bf88786
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions js/src/2024/10/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable no-cond-assign */

import { Grid, cast, solution } from '../../utils/index.js';

const parse = (input) => Grid.from(input, { cast });

const explore = (grid) => {
const zeroes = grid.filter((point) => point.value === 0);

const frontier = zeroes.map((zero) => ({ current: zero, start: zero }));

let entry;
const trails = [];
while ((entry = frontier.pop())) {
const { current, start } = entry;

if (current.value === 9) {
trails.push({ start, end: current });
continue;
}

for (const candidate of current.adjacentNeighbors) {
if (current.value + 1 === candidate.value) {
frontier.push({ current: candidate, start });
}
}
}
return trails;
};

const normalize = (trails) => {
const set = new Set();
for (const { start, end } of trails) {
set.add(`${start.x}:${start.y} => ${end.x}:${end.y}`);
}
return Array.from(set);
};

export const partOne = solution((input) => {
const grid = parse(input);
const trails = explore(grid);
return normalize(trails).length;
});

export const partTwo = solution((input) => {
const grid = parse(input);
const trails = explore(grid);
return trails.length;
});
49 changes: 49 additions & 0 deletions puzzles/2024/10/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
part-one:
- input: |
0123
1234
8765
9876
answer: 1
- input: |
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
answer: 4
- input: |
10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01
answer: 3
- input: |
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
answer: 36
part-two:
- input: |
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
answer: 13

0 comments on commit bf88786

Please sign in to comment.