Skip to content

Commit

Permalink
Year 2024, Day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 13, 2024
1 parent 4b5a131 commit c993967
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions clients/typescript/solutions/S2413.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ISolution from "./ISolution.ts";
import * as mathjs from "npm:mathjs";

export default class S2413 implements ISolution {
firstPart(input: string): (number | string) | Promise<number | string> {
const prizes = input.split("\n\n")
.map(prize => prize.split("\n").map(l => l.split(": ")[ 1 ].split(", ")))
.map(([ a, b, prize ]) => ({ a: a.map(o => Number(o.slice(1))), b: b.map(o => Number(o.slice(1))), prize: prize.map(p => Number(p.slice(2))) }));
let sum = 0;
for (const prize of prizes) {
const options = [];
for (let ia = 0; ia < 100; ia++) {
for (let ib = 0; ib < 100; ib++) {
const x = ia * prize.a[ 0 ] + ib * prize.b[ 0 ];
const y = ia * prize.a[ 1 ] + ib * prize.b[ 1 ];
if (x === prize.prize[ 0 ] && y === prize.prize[ 1 ]) {
options.push(ia * 3 + ib);
}
}
}
if (options.length !== 0) {
sum += Math.min(...options);
}
}
return sum;
}
secondPart(input: string) {
const prizes = input.split("\n\n")
.map(prize => prize.split("\n").map(l => l.split(": ")[ 1 ].split(", ")))
.map(([ a, b, prize ]) => ({ a: a.map(o => Number(o.slice(1))), b: b.map(o => Number(o.slice(1))), prize: prize.map(p => 10000000000000 + Number(p.slice(2))) }));
let sum = 0;
for (const prize of prizes) {
// prize.a[ 0 ] * x + prize.b[ 0 ] * y = prize.prize[ 0 ]
// prize.a[ 1 ] * x + prize.b[ 1 ] * y = prize.prize[ 1 ]
const left = [
[ prize.a[ 0 ], prize.b[ 0 ] ],
[ prize.a[ 1 ], prize.b[ 1 ] ]
];

const right = [
prize.prize[ 0 ],
prize.prize[ 1 ]
];

const solution = mathjs.lusolve(left, right);
const [ x, y ] = solution.map(x => Number(x[ 0 ]));
if ((x % 1 < 0.001 || x % 1 > 0.999) && (y % 1 > 0.999 || y % 1 < 0.001)) { // Fuck floating point
if (x > 0 && y > 0) {
sum += Math.round(x) * 3 + Math.round(y);
}
}
}

return sum;
}

}

0 comments on commit c993967

Please sign in to comment.