mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 20:49:24 +00:00
2024 day 13
This commit is contained in:
40
2024/day-13/day13.test.ts
Normal file
40
2024/day-13/day13.test.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { expect, test } from "bun:test";
|
||||||
|
import * as path from "node:path";
|
||||||
|
import { part1, part2 } from "./day13.ts";
|
||||||
|
|
||||||
|
test("day 13, part 1", async () => {
|
||||||
|
const testInput = await Bun.file(
|
||||||
|
path.resolve(__dirname, "test-input.txt"),
|
||||||
|
).text();
|
||||||
|
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||||
|
|
||||||
|
console.log("\n\n");
|
||||||
|
|
||||||
|
const testResult = part1(testInput);
|
||||||
|
console.log("Test data:", testResult);
|
||||||
|
expect(testResult).toEqual(480);
|
||||||
|
|
||||||
|
const finalResult = part1(input);
|
||||||
|
console.log("Full data:", finalResult);
|
||||||
|
expect(finalResult).toEqual(27105);
|
||||||
|
|
||||||
|
console.log("\n\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("day 13, part 2", async () => {
|
||||||
|
const testInput = await Bun.file(
|
||||||
|
path.resolve(__dirname, "test-input.txt"),
|
||||||
|
).text();
|
||||||
|
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||||
|
|
||||||
|
const testResult = part2(testInput);
|
||||||
|
console.log("\n\n");
|
||||||
|
console.log("Test data:", testResult);
|
||||||
|
expect(testResult).toEqual(875318608908);
|
||||||
|
|
||||||
|
const finalResult = part2(input);
|
||||||
|
console.log("Full data:", finalResult);
|
||||||
|
expect(finalResult).toEqual(101726882250942);
|
||||||
|
|
||||||
|
console.log("\n\n");
|
||||||
|
});
|
||||||
69
2024/day-13/day13.ts
Normal file
69
2024/day-13/day13.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
const TOKENS_PER_PUSH_A = 3;
|
||||||
|
const TOKENS_PER_PUSH_B = 1;
|
||||||
|
|
||||||
|
export function part1(input: string) {
|
||||||
|
const configs = parseInput(input);
|
||||||
|
return solve(configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function part2(input: string) {
|
||||||
|
const configs = parseInput(input).map((o) => {
|
||||||
|
return {
|
||||||
|
...o,
|
||||||
|
prize: o.prize.map((x) => x + 10000000000000),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return solve(configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
function solve(parsed: ReturnType<typeof parseInput>) {
|
||||||
|
let final = 0;
|
||||||
|
parsed.forEach((config) => {
|
||||||
|
const options = findOptions(config);
|
||||||
|
if (options.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final += options[0] * TOKENS_PER_PUSH_A + options[1] * TOKENS_PER_PUSH_B;
|
||||||
|
});
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findOptions(config: ReturnType<typeof parseInput>[number]) {
|
||||||
|
const [ax, ay] = config.buttonA;
|
||||||
|
const [bx, by] = config.buttonB;
|
||||||
|
const [gx, gy] = config.prize;
|
||||||
|
const ca = (gx * by - gy * bx) / (ax * by - ay * bx);
|
||||||
|
if (!isInt(ca)) return [];
|
||||||
|
return [ca, (gx - ca * ax) / bx];
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseInput(input: string) {
|
||||||
|
const parts = input.split("\n\n");
|
||||||
|
|
||||||
|
return parts.map((part) => {
|
||||||
|
const [partA, partB, partResult] = part.split("\n");
|
||||||
|
const buttonA = partA
|
||||||
|
.replace("Button A: ", "")
|
||||||
|
.replace("X+", "")
|
||||||
|
.replace(" Y+", "")
|
||||||
|
.split(",")
|
||||||
|
.map(Number);
|
||||||
|
const buttonB = partB
|
||||||
|
.replace("Button B: ", "")
|
||||||
|
.replace("X+", "")
|
||||||
|
.replace(" Y+", "")
|
||||||
|
.split(",")
|
||||||
|
.map(Number);
|
||||||
|
const prize = partResult
|
||||||
|
.replace("Prize: ", "")
|
||||||
|
.replace("X=", "")
|
||||||
|
.replace(" Y=", "")
|
||||||
|
.split(",")
|
||||||
|
.map(Number);
|
||||||
|
return { buttonA, buttonB, prize };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function isInt(number: number) {
|
||||||
|
return number % 1 === 0;
|
||||||
|
}
|
||||||
1279
2024/day-13/input.txt
Normal file
1279
2024/day-13/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
2024/day-13/test-input.txt
Normal file
15
2024/day-13/test-input.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
Reference in New Issue
Block a user