Skip to content

Commit

Permalink
day 13!
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Dec 13, 2024
1 parent 603542c commit 9dc86c5
Show file tree
Hide file tree
Showing 35 changed files with 6,418 additions and 6,028 deletions.
Binary file modified aoc/aoc_data.db
Binary file not shown.
414 changes: 207 additions & 207 deletions index.html

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions puzzles/2024/day12/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ const floodFill = (grid: string[], details: { area: number, perimeter: { [key: s
if (x !== width - 1 && grid[y][x + 1] === plant) floodFill(grid, details, x + 1, y, area);
}

/**
* remove extra perimeter based on axis
*/
const filterPerimeters = (array: Perimeter[], primary: 'x' | 'y', secondary: 'x' | 'y') => {
// sort from smallest to largest
array.sort((a, b) => a[primary] - b[primary]);

// try to remove any perimeters on the same line
for (let i = 0; i < array.length; i++) {
let check = array[i][primary];
while (true) {
check++;
const nextNode = array.find(node => node[primary] === check && node[secondary] === array[i][secondary]);

// end check if not on continuous line
if (nextNode !== undefined) nextNode.valid = false;
else break;
}
}
}

/**
* the code of part 1 of the puzzle
*/
Expand Down Expand Up @@ -73,27 +94,6 @@ const part2 = (input: string) => {
const grid = input.trim().split('\n');
const width = grid[0].length, height = grid.length;

/**
* remove extra perimeter based on axis
*/
const filterPerimeters = (array: Perimeter[], primary: 'x' | 'y', secondary: 'x' | 'y') => {
// sort from smallest to largest
array.sort((a, b) => a[primary] - b[primary]);

// try to remove any perimeters on the same line
for (let i = 0; i < array.length; i++) {
let check = array[i][primary];
while (true) {
check++;
const nextNode = array.find(node => node[primary] === check && node[secondary] === array[i][secondary]);

// end check if not on continuous line
if (nextNode !== undefined) nextNode.valid = false;
else break;
}
}
}

let alreadyFlooded = new Set<string>();
let sum = 0;
for (let y = 0; y < height; y++) {
Expand Down
29 changes: 29 additions & 0 deletions puzzles/2024/day13/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Advent of Code 2024 - Day 13: [Claw Contraption](https://adventofcode.com/2024/day/13)

## [Write Up](https://codingap.github.io/advent-of-code/writeups/2024/day13)

## Results

| | **Part 1** | **Part 2** |
| :--------------: | :--------: | :--------: |
| **Results** | 28753 | 9144671599845478000 |
| **Time (in ms)** | 1.49 | 2.05 |

%%%

Leaderboard Positions - **Part 1**: 2316, **Part 2**: 679

[Video Replay](https://youtu.be/6QdOUQeiPCk)

Hello all! In this puzzle, we are trying to figure out the exact number of button presses to reach a goal in a crane machine. Each crane has two buttons labeled `A` and `B`, which both buttons advancing the crane arm a specific amount. We need to find how many of each button press will lead to the position being exactly the goal. In part 1, we try to find the amount, then add all the successful `A` and `B` numbers with this formula: `3 * A + B`. To prevent bruteforcing, we treat each button as a system of linear equations, which we can solve as there are two variables and two equation. I made a [Desmos](https://www.desmos.com/calculator/l1amw7m2rg) that calculates the `A` and `B` given the constants. To map the constant for completion sake:

```
c1: button a x
c2: button b x
c3: prize x
c4: button a y
c5: button b y
c6: prize y
```

For part 2, we need to add `10000000000000` to each of the prize positions, which with our current solution shouldn't be a problem. One thing to note is make sure to keep floating point inaccuracies down, so do as much integer arithetic as possible
63 changes: 63 additions & 0 deletions puzzles/2024/day13/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* puzzles/2024/day13/solution.ts
*
* ~~ Claw Contraption ~~
* this is my solution for this advent of code puzzle
*
* by alex prosser
* 12/12/2024
*/

/**
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
// parse the constants out of each test
const machines = input.trim().split('\n\n').map(lines => {
const [buttonA, buttonB, prize] = lines.split('\n');
const [c1, c4] = buttonA.split(': ')[1].split(', ').map(num => parseInt(num.split('+')[1]));
const [c2, c5] = buttonB.split(': ')[1].split(', ').map(num => parseInt(num.split('+')[1]));
const [c3, c6] = prize.split(': ')[1].split(', ').map(num => parseInt(num.split('=')[1]));
return { c1, c2, c3, c4, c5, c6 };
});

return machines.reduce((sum, machine, i) => {
// try to calculate a and b
// c3 = c1 * a + c2 * b
// c6 = c4 * a + c5 * b
// https://www.desmos.com/calculator/l1amw7m2rg
const b = (machine.c1 * machine.c6 - machine.c4 * machine.c3) / (machine.c1 * machine.c5 - machine.c4 * machine.c2);
const a = (machine.c3 - machine.c2 * b) / machine.c1;

// test is accepted when both numbers are integers
return sum + ((a % 1 === 0 && b % 1 === 0) ? (a * 3 + b) : 0);
}, 0);
};

/**
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
// parse the constants out of each machine, add 10000000000000 to the prize position
const machines = input.trim().split('\n\n').map(lines => {
const [buttonA, buttonB, prize] = lines.split('\n');
const [c1, c4] = buttonA.split(': ')[1].split(', ').map(num => parseInt(num.split('+')[1]));
const [c2, c5] = buttonB.split(': ')[1].split(', ').map(num => parseInt(num.split('+')[1]));
const [c3, c6] = prize.split(': ')[1].split(', ').map(num => parseInt(num.split('=')[1]) + 10000000000000);
return { c1, c2, c3, c4, c5, c6 };
});

return machines.reduce((sum, machine) => {
// try to calculate a and b
// c3 = c1 * a + c2 * b
// c6 = c4 * a + c5 * b
// https://www.desmos.com/calculator/l1amw7m2rg
const b = (machine.c1 * machine.c6 - machine.c4 * machine.c3) / (machine.c1 * machine.c5 - machine.c4 * machine.c2);
const a = (machine.c3 - machine.c2 * b) / machine.c1;

// test is accepted when both numbers are integers
return sum + ((a % 1 === 0 && b % 1 === 0) ? (a * 3 + b) : 0);
}, 0);
};

export { part1, part2 };
Loading

0 comments on commit 9dc86c5

Please sign in to comment.