Skip to content

Commit

Permalink
day 10!
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Dec 10, 2024
1 parent 8b57a10 commit 60d6855
Show file tree
Hide file tree
Showing 33 changed files with 5,795 additions and 5,462 deletions.
Binary file modified aoc/aoc_data.db
Binary file not shown.
432 changes: 216 additions & 216 deletions index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion puzzles/2021/day15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
| | **Part 1** | **Part 2** |
| :--------------: | :--------: | :--------: |
| **Results** | 398 | 2817 |
| **Time (in ms)** | 14.44 | 570.00 |
| **Time (in ms)** | 12.41 | 423.58 |
63 changes: 21 additions & 42 deletions puzzles/2021/day15/solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2021/day15/solution.ts
*
Expand All @@ -10,33 +8,24 @@
* 11/27/2024
*/

const create2DArray = (width, height, fill) => {
let array = new Array(height).fill('').map(_ => new Array(width));

for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (typeof fill === 'function') array[y][x] = fill(x, y);
else array[y][x] = fill;
}
}

return array;
};
interface MinHeapNode {
score: number;
data: { [key: string]: any };
}

/**
* min heap implementation
*/
class MinHeap {
heap: MinHeapNode[];
constructor() {
this.heap = [];
}

/**
* inserts an element and heapifies it until it is in the correct location
*
* @param {{ score: number, data: { [key: string]: any } }} element element to insert with the score
*/
insert(element) {
insert(element: MinHeapNode) {
this.heap.push(element);
let index = this.heap.length - 1;

Expand All @@ -50,13 +39,11 @@ class MinHeap {

/**
* gets the smallest element, which is at the beginning, then heapifies
*
* @returns {{ score: number, data: { [key: string]: any } }} the smallest scored element
*/
extractMin() {
if (this.heap.length === 1) return this.heap.pop();
if (this.heap.length === 1) return this.heap.pop() as MinHeapNode;
const min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heap[0] = this.heap.pop() as MinHeapNode;
let index = 0;

while (true) {
Expand All @@ -76,8 +63,6 @@ class MinHeap {

/**
* return the size of the heap
*
* @returns {number} the size of the heap
*/
size() {
return this.heap.length;
Expand All @@ -86,13 +71,8 @@ class MinHeap {

/**
* finds the best path from the starting point to the ending point
*
* @param {number[][]} grid maze grid with the levels
* @param {{ x: number, y: number }} start starting point and level
* @param {{ x: number, y: number }} end ending point and level
* @returns {number} how many units it takes to travel
*/
const solveMazeWeighted = (grid, start, end) => {
const findPath = (grid: number[][], start: { x: number, y: number }, end: { x: number, y: number }): number => {
const minHeap = new MinHeap();
minHeap.insert({ score: 0, data: { ...start } });

Expand Down Expand Up @@ -132,29 +112,28 @@ const solveMazeWeighted = (grid, start, end) => {
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
let rows = input.trim().split('\n');
let grid = create2DArray(rows[0].length, rows.length, (x, y) => {
return parseInt(rows[y][x]);
});

return solveMazeWeighted(grid, { x: 0, y: 0 }, { x: grid[0].length - 1, y: grid[1].length - 1 });
const grid = input.trim().split('\n').map(line => line.split('').map(num => parseInt(num)));
return findPath(grid, { x: 0, y: 0 }, { x: grid[0].length - 1, y: grid[1].length - 1 });
};

/**
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
let rows = input.trim().split('\n');
let grid = create2DArray(rows[0].length * 5, rows.length * 5, (x, y) => {
let quadrantX = Math.floor(x / rows[0].length);
let quadrantY = Math.floor(y / rows.length);
const smallGrid = input.trim().split('\n').map(line => line.split(''));
const width = smallGrid[0].length, height = smallGrid.length;
const bigWidth = width * 5, bigHeight = height * 5;

const bigGrid = new Array(bigHeight).fill('').map((_, y) => new Array(bigWidth).fill('').map((__, x) => {
let quadrantX = Math.floor(x / height);
let quadrantY = Math.floor(y / width);

let value = parseInt(rows[y - (quadrantY * rows.length)][x - (quadrantX * rows[0].length)]) + (quadrantX + quadrantY);
let value = parseInt(smallGrid[y - (quadrantY * width)][x - (quadrantX * height)]) + (quadrantX + quadrantY);
if (value > 9) value -= 9;
return value;
});
}));

return solveMazeWeighted(grid, { x: 0, y: 0 }, { x: grid[0].length - 1, y: grid[1].length - 1 });
return findPath(bigGrid, { x: 0, y: 0 }, { x: bigWidth - 1, y: bigHeight - 1 });
};

export { part1, part2 };
18 changes: 18 additions & 0 deletions puzzles/2024/day10/README.md
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.
58 changes: 55 additions & 3 deletions puzzles/2024/day10/solution.ts
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 };
Loading

0 comments on commit 60d6855

Please sign in to comment.