-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
5,795 additions
and
5,462 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,18 @@ | ||
# Advent of Code 2024 - Day 10: [Hoof It](https://adventofcode.com/2024/day/10) | ||
|
||
## [Write Up](https://codingap.github.io/advent-of-code/writeups/2024/day10) | ||
|
||
## Results | ||
|
||
| | **Part 1** | **Part 2** | | ||
| :--------------: | :--------: | :--------: | | ||
| **Results** | 635 | 800 | | ||
| **Time (in ms)** | 2.97 | 2.25 | | ||
|
||
%%% | ||
|
||
Leaderboard Positions - **Part 1**: 5114, **Part 2**: 3147 | ||
|
||
[Video Replay](https://youtu.be/LKCjL2le8WA) | ||
|
||
Hello all! In this puzzle, we are finding the score and rating of different trailheads. A trail is a path starting at `0` (the trailhead) going to `9` (the trailtail) by only incrementing by 1 each step (ex. 0,1,2,3,4,5,6,7,8,9). In part 1, we need to find the score of each trailhead, which is how many trailtails it can access. In part 2, we need to find the rating, which is how many unique trails can be created from a trailhead. In both parts, we can use a graph traversal algorithm like BFS or DFS to find all valid paths. The only different is whether we prevent repeat searches or not. |
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 |
---|---|---|
@@ -1,25 +1,77 @@ | ||
/** | ||
* puzzles/2024/day10/solution.ts | ||
* | ||
* ~~ no title available! ~~ | ||
* ~~ Hoof It ~~ | ||
* this is my solution for this advent of code puzzle | ||
* | ||
* by alex prosser | ||
* 12/9/2024 | ||
*/ | ||
|
||
/** | ||
* run bfs on each trailhead to find all paths, unique or not depending on part | ||
*/ | ||
const findTrails = (grid: number[][], starting: { x: number, y: number }, part1: boolean): number => { | ||
const width = grid[0].length, height = grid.length; | ||
const queue: { x: number, y: number }[] = [{ ...starting }]; | ||
const visited = new Set<string>(); // only use visited in part 1 | ||
|
||
// count all possible paths | ||
let paths = 0; | ||
while (queue.length != 0) { | ||
const current = queue.shift(); | ||
if (current === undefined) break; | ||
|
||
if (grid[current.y][current.x] === 9) { | ||
paths++; | ||
continue; | ||
} | ||
|
||
// try all directions | ||
[{ x: 0, y: -1 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 0 }].forEach(direction => { | ||
const position = { x: current.x + direction.x, y: current.y + direction.y }; | ||
if (position.x < 0 || position.x >= width || position.y < 0 || position.y >= height || (visited.has(`${position.x},${position.y}`) && part1) || grid[position.y][position.x] - grid[current.y][current.x] !== 1) return; | ||
|
||
queue.push(position); | ||
if (part1) visited.add(`${position.x},${position.y}`); | ||
}); | ||
} | ||
|
||
return paths; | ||
} | ||
|
||
/** | ||
* the code of part 1 of the puzzle | ||
*/ | ||
const part1 = (input: string) => { | ||
return 0; | ||
const grid = input.split('\n').map(line => line.split('').map(num => parseInt(num))); | ||
const width = grid[0].length, height = grid.length; | ||
|
||
// add all trailhead scores | ||
let sum = 0; | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
if (grid[y][x] === 0) sum += findTrails(grid, { x, y }, true); | ||
} | ||
} | ||
return sum; | ||
}; | ||
|
||
/** | ||
* the code of part 2 of the puzzle | ||
*/ | ||
const part2 = (input: string) => { | ||
return 0; | ||
const grid = input.split('\n').map(line => line.split('').map(num => parseInt(num))); | ||
const width = grid[0].length, height = grid.length; | ||
|
||
// add all trailhead ratings | ||
let sum = 0; | ||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
if (grid[y][x] === 0) sum += findTrails(grid, { x, y }, false); | ||
} | ||
} | ||
return sum; | ||
}; | ||
|
||
export { part1, part2 }; |
Oops, something went wrong.