-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add solutions for 2024 day 10
- Loading branch information
1 parent
3a6d7c2
commit bf88786
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |