Skip to content

Commit 09c0c7c

Browse files
committed
feat: 2024 Day 13
1 parent 410a2e7 commit 09c0c7c

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

2024/day/13/part/1/solve.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "../../../../../lib/testing/asserts.ts";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
Button A: X+94, Y+34
8+
Button B: X+22, Y+67
9+
Prize: X=8400, Y=5400
10+
11+
Button A: X+26, Y+66
12+
Button B: X+67, Y+21
13+
Prize: X=12748, Y=12176
14+
15+
Button A: X+17, Y+86
16+
Button B: X+84, Y+37
17+
Prize: X=7870, Y=6450
18+
19+
Button A: X+69, Y+23
20+
Button B: X+27, Y+71
21+
Prize: X=18641, Y=10279`;
22+
23+
assertEquals(solve(input), 480);
24+
});

2024/day/13/part/1/solve.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const regExp =
2+
/^Button A: X\+(?<ax>\d+), Y\+(?<ay>\d+)$\n^Button B: X\+(?<bx>\d+), Y\+(?<by>\d+)$\n^Prize: X=(?<px>\d+), Y=(?<py>\d+)$/gm;
3+
4+
export default function solve(input: string) {
5+
const configs = input.matchAll(regExp).map((array) => array.map(Number))
6+
.map(([, ax, ay, bx, by, px, py]) => ({ ax, ay, bx, by, px, py }))
7+
.toArray();
8+
let tokens = 0;
9+
for (const { ax, ay, bx, by, px, py } of configs) {
10+
let minTokens = Infinity;
11+
for (let a = 0; a < 100; a++) {
12+
for (let b = 0; b < 100; b++) {
13+
const x = a * ax + b * bx, y = a * ay + b * by, $ = 3 * a + b;
14+
if (x === px && y === py && $ < minTokens) minTokens = $;
15+
}
16+
}
17+
if (minTokens !== Infinity) tokens += minTokens;
18+
}
19+
return tokens;
20+
}

2024/day/13/part/2/solve.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "../../../../../lib/testing/asserts.ts";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
Button A: X+94, Y+34
8+
Button B: X+22, Y+67
9+
Prize: X=8400, Y=5400
10+
11+
Button A: X+26, Y+66
12+
Button B: X+67, Y+21
13+
Prize: X=12748, Y=12176
14+
15+
Button A: X+17, Y+86
16+
Button B: X+84, Y+37
17+
Prize: X=7870, Y=6450
18+
19+
Button A: X+69, Y+23
20+
Button B: X+27, Y+71
21+
Prize: X=18641, Y=10279`;
22+
23+
assertEquals(solve(input, { offset: 0 }), 480);
24+
});

2024/day/13/part/2/solve.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const regExp =
2+
/^Button A: X\+(?<ax>\d+), Y\+(?<ay>\d+)$\n^Button B: X\+(?<bx>\d+), Y\+(?<by>\d+)$\n^Prize: X=(?<px>\d+), Y=(?<py>\d+)$/gm;
3+
4+
export default function solve(input: string, { offset = 10000000000000 } = {}) {
5+
const configs = input.matchAll(regExp).map((array) => array.map(Number))
6+
.map(([, ax, ay, bx, by, px, py]) => ({ ax, ay, bx, by, px, py }))
7+
.toArray();
8+
for (const config of configs) config.px += offset, config.py += offset;
9+
let tokens = 0;
10+
for (const { ax, ay, bx, by, px, py } of configs) {
11+
const d = ax * by - bx * ay;
12+
const at = (px * by - py * bx) / d, bt = (py * ax - px * ay) / d;
13+
if (Number.isInteger(at) && Number.isInteger(bt)) tokens += 3 * at + bt;
14+
}
15+
return tokens;
16+
}

0 commit comments

Comments
 (0)