-
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 20 part 1
- Loading branch information
1 parent
d7ed943
commit a2b3204
Showing
2 changed files
with
71 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,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 ;) |
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,20 @@ | ||
part-one: | ||
- input: | | ||
############### | ||
#...#...#.....# | ||
#.#.#.#.#.###.# | ||
#S#...#.#.#...# | ||
#######.#.#.### | ||
#######.#.#...# | ||
#######.#.###.# | ||
###..E#...#...# | ||
###.#######.### | ||
#...###...#...# | ||
#.#####.#.###.# | ||
#.#...#.#.#...# | ||
#.#.#.#.#.#.### | ||
#...#...#...### | ||
############### | ||
answer: 4 | ||
args: | ||
min-savings: 30 |