mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 09:06:09 +00:00
day 13
This commit is contained in:
parent
5e26df54c8
commit
b22d060ef6
4 changed files with 128 additions and 16 deletions
|
@ -191,16 +191,20 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "28887",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"0"
|
||||||
|
],
|
||||||
|
"time": 1984.919458
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "96979582619758",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"5923913045842"
|
||||||
|
],
|
||||||
|
"time": 6.418125
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
16
README.md
16
README.md
|
@ -23,7 +23,7 @@
|
||||||
[](src/day10)
|
[](src/day10)
|
||||||
[](src/day11)
|
[](src/day11)
|
||||||
[](src/day12)
|
[](src/day12)
|
||||||

|
[](src/day13)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -146,16 +146,16 @@ Both parts: 130.69ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 12
|
Day 12
|
||||||
Time part 1: -
|
Time part 1: 172.252ms
|
||||||
Time part 2: 220.714ms
|
Time part 2: 214.984ms
|
||||||
Both parts: 220.714ms
|
Both parts: 387.236ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 13
|
Day 13
|
||||||
Time part 1: -
|
Time part 1: -
|
||||||
Time part 2: -
|
Time part 2: 7.731ms
|
||||||
Both parts: -
|
Both parts: 7.731ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 24/50
|
Total stars: 26/50
|
||||||
Total time: 38735.743ms
|
Total time: 38909.996ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day13/README.md
Normal file
9
src/day13/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2024 - day 13 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2024/day/13)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
99
src/day13/index.ts
Normal file
99
src/day13/index.ts
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
const parseInput = (rawInput: string) =>
|
||||||
|
rawInput.split("\n\n").map((machine) => {
|
||||||
|
return machine
|
||||||
|
.split("\n")
|
||||||
|
.map((l) => Array.from(l.matchAll(/\d+/g)).map(Number))
|
||||||
|
})
|
||||||
|
|
||||||
|
const part1 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
let spent = 0
|
||||||
|
for (const machine of input) {
|
||||||
|
const [[adx, ady], [bdx, bdy], [prizex, prizey]] = machine
|
||||||
|
let minCost = Number.POSITIVE_INFINITY
|
||||||
|
const tried = new Set<string>()
|
||||||
|
const simulate = (
|
||||||
|
x: number,
|
||||||
|
y: number,
|
||||||
|
aPresses: number,
|
||||||
|
bPresses: number,
|
||||||
|
) => {
|
||||||
|
const key = [x, y, aPresses, bPresses].join()
|
||||||
|
if (tried.has(key)) return
|
||||||
|
tried.add(key)
|
||||||
|
if (x > prizex || y > prizey) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const cost = aPresses * 3 + bPresses
|
||||||
|
if (cost >= minCost) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (x === prizex && y === prizey) {
|
||||||
|
minCost = cost
|
||||||
|
return
|
||||||
|
}
|
||||||
|
simulate(x + adx, y + ady, aPresses + 1, bPresses)
|
||||||
|
simulate(x + bdx, y + bdy, aPresses, bPresses + 1)
|
||||||
|
}
|
||||||
|
simulate(0, 0, 0, 0)
|
||||||
|
if (minCost < Number.POSITIVE_INFINITY) {
|
||||||
|
spent += minCost
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return spent
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
let spent = 0
|
||||||
|
for (const machine of input) {
|
||||||
|
const [[adx, ady], [bdx, bdy], [specx, specy]] = machine
|
||||||
|
const prizex = 10000000000000 + specx
|
||||||
|
const prizey = 10000000000000 + specy
|
||||||
|
const bPresses = (prizex * ady - prizey * adx) / (bdx * ady - bdy * adx)
|
||||||
|
const aPresses = (prizey - bPresses * bdy) / ady
|
||||||
|
console.log({ aPresses, bPresses })
|
||||||
|
if (bPresses % 1 === 0 && aPresses % 1 === 0) {
|
||||||
|
spent += 3 * aPresses + bPresses
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return spent
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `Button A: X+94, Y+34
|
||||||
|
Button B: X+22, Y+67
|
||||||
|
Prize: X=8400, Y=5400
|
||||||
|
|
||||||
|
Button A: X+26, Y+66
|
||||||
|
Button B: X+67, Y+21
|
||||||
|
Prize: X=12748, Y=12176
|
||||||
|
|
||||||
|
Button A: X+17, Y+86
|
||||||
|
Button B: X+84, Y+37
|
||||||
|
Prize: X=7870, Y=6450
|
||||||
|
|
||||||
|
Button A: X+69, Y+23
|
||||||
|
Button B: X+27, Y+71
|
||||||
|
Prize: X=18641, Y=10279`,
|
||||||
|
expected: 480,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
// {
|
||||||
|
// input: ``,
|
||||||
|
// expected: "",
|
||||||
|
// },
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
onlyTests: false,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue