Skip to content

Commit

Permalink
day-13 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
spalberg authored Dec 20, 2024
1 parent a4161f0 commit e54ff44
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
43 changes: 43 additions & 0 deletions days/13/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { loadInput } from "utils";

export function part1(input: Array<string>) {
return solve(input);
}

export function part2(input: Array<string>) {
return solve(input, 100_000_000_000_00);
}

function solve(input: Array<string>, dp = 0) {
let total = 0;
for (const { xa, ya, xb, yb, xp, yp } of parseInput(input, dp)) {
const b = (xa * yp - xp * ya) / (xa * yb - xb * ya);
const a = (yp - b * yb) / ya;
if (!Number.isInteger(a) || !Number.isInteger(b)) {
continue;
}
total += 3 * a + b;
}
return total;
}

function* parseInput(input: Array<string>, dp: number) {
for (let i = 0; i < input.length; i += 4) {
const buttonA = input[i].match(/Button A: X\+(\d+), Y\+(\d+)/)!;
const buttonB = input[i + 1].match(/Button B: X\+(\d+), Y\+(\d+)/)!;
const prize = input[i + 2].match(/Prize: X=(\d+), Y=(\d+)/)!;
yield {
xa: parseInt(buttonA[1], 10),
ya: parseInt(buttonA[2], 10),
xb: parseInt(buttonB[1], 10),
yb: parseInt(buttonB[2], 10),
xp: parseInt(prize[1], 10) + dp,
yp: parseInt(prize[2], 10) + dp,
};
}
}

if (import.meta.main) {
console.log(part1(await loadInput(13)));
console.log(part2(await loadInput(13)));
}
42 changes: 42 additions & 0 deletions days/13/main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from "@std/expect";
import { describe, it } from "@std/testing/bdd";
import { part1, part2 } from "./main.ts";
import { loadInput } from "utils";

describe("day 13 example", () => {
const 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",
];

it("part 1", () => {
expect(part1(input)).toBe(480);
});

it("part 2", () => {
expect(part2(input)).toBe(875318608908);
});
});

describe("day 13 solution", () => {
it("part 1", async () => {
expect(part1(await loadInput(13))).toBe(37680);
});

it("part 2", async () => {
expect(part2(await loadInput(13))).toBe(87550094242995);
});
});
2 changes: 1 addition & 1 deletion days/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Solution = { part1: PartFunction; part2: PartFunction };

const days = new Map<number, Solution>();

for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) {
for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13]) {
days.set(day, await import(`./${day}/main.ts`));
}

Expand Down
2 changes: 1 addition & 1 deletion inputs

0 comments on commit e54ff44

Please sign in to comment.