Skip to content

Commit c993967

Browse files
committed
Year 2024, Day 13
1 parent 4b5a131 commit c993967

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

clients/typescript/solutions/S2413.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import ISolution from "./ISolution.ts";
2+
import * as mathjs from "npm:mathjs";
3+
4+
export default class S2413 implements ISolution {
5+
firstPart(input: string): (number | string) | Promise<number | string> {
6+
const prizes = input.split("\n\n")
7+
.map(prize => prize.split("\n").map(l => l.split(": ")[ 1 ].split(", ")))
8+
.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))) }));
9+
let sum = 0;
10+
for (const prize of prizes) {
11+
const options = [];
12+
for (let ia = 0; ia < 100; ia++) {
13+
for (let ib = 0; ib < 100; ib++) {
14+
const x = ia * prize.a[ 0 ] + ib * prize.b[ 0 ];
15+
const y = ia * prize.a[ 1 ] + ib * prize.b[ 1 ];
16+
if (x === prize.prize[ 0 ] && y === prize.prize[ 1 ]) {
17+
options.push(ia * 3 + ib);
18+
}
19+
}
20+
}
21+
if (options.length !== 0) {
22+
sum += Math.min(...options);
23+
}
24+
}
25+
return sum;
26+
}
27+
secondPart(input: string) {
28+
const prizes = input.split("\n\n")
29+
.map(prize => prize.split("\n").map(l => l.split(": ")[ 1 ].split(", ")))
30+
.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))) }));
31+
let sum = 0;
32+
for (const prize of prizes) {
33+
// prize.a[ 0 ] * x + prize.b[ 0 ] * y = prize.prize[ 0 ]
34+
// prize.a[ 1 ] * x + prize.b[ 1 ] * y = prize.prize[ 1 ]
35+
const left = [
36+
[ prize.a[ 0 ], prize.b[ 0 ] ],
37+
[ prize.a[ 1 ], prize.b[ 1 ] ]
38+
];
39+
40+
const right = [
41+
prize.prize[ 0 ],
42+
prize.prize[ 1 ]
43+
];
44+
45+
const solution = mathjs.lusolve(left, right);
46+
const [ x, y ] = solution.map(x => Number(x[ 0 ]));
47+
if ((x % 1 < 0.001 || x % 1 > 0.999) && (y % 1 > 0.999 || y % 1 < 0.001)) { // Fuck floating point
48+
if (x > 0 && y > 0) {
49+
sum += Math.round(x) * 3 + Math.round(y);
50+
}
51+
}
52+
}
53+
54+
return sum;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)