-
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
35 changed files
with
6,418 additions
and
6,028 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
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 |
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,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 }; |
Oops, something went wrong.