diff --git a/js/src/2024/10/index.js b/js/src/2024/10/index.js new file mode 100644 index 0000000..0db7999 --- /dev/null +++ b/js/src/2024/10/index.js @@ -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; +}); diff --git a/puzzles/2024/10/examples.yaml b/puzzles/2024/10/examples.yaml new file mode 100644 index 0000000..b44a35a --- /dev/null +++ b/puzzles/2024/10/examples.yaml @@ -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