diff --git a/.aocrunner.json b/.aocrunner.json index a030a37..475287c 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -191,16 +191,20 @@ }, { "part1": { - "solved": false, - "result": null, - "attempts": [], - "time": null + "solved": true, + "result": "28887", + "attempts": [ + "0" + ], + "time": 1984.919458 }, "part2": { - "solved": false, - "result": null, - "attempts": [], - "time": null + "solved": true, + "result": "96979582619758", + "attempts": [ + "5923913045842" + ], + "time": 6.418125 } }, { diff --git a/README.md b/README.md index 526cc36..b0f8859 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ [![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10) [![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11) [![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12) -![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/13/%E2%98%85%E2%98%85/green)](src/day13) ![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/16/%E2%98%86%E2%98%86/gray) @@ -146,16 +146,16 @@ Both parts: 130.69ms ``` Day 12 -Time part 1: - -Time part 2: 220.714ms -Both parts: 220.714ms +Time part 1: 172.252ms +Time part 2: 214.984ms +Both parts: 387.236ms ``` ``` Day 13 Time part 1: - -Time part 2: - -Both parts: - +Time part 2: 7.731ms +Both parts: 7.731ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 24/50 -Total time: 38735.743ms +Total stars: 26/50 +Total time: 38909.996ms ``` diff --git a/src/day13/README.md b/src/day13/README.md new file mode 100644 index 0000000..e6c852b --- /dev/null +++ b/src/day13/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2024 - day 13 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2024/day/13) + +## Notes + +... \ No newline at end of file diff --git a/src/day13/index.ts b/src/day13/index.ts new file mode 100644 index 0000000..395469d --- /dev/null +++ b/src/day13/index.ts @@ -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() + 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, +})