-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.ts
38 lines (33 loc) · 923 Bytes
/
day13.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { type MainArgs, parseFile } from './lib/utils.ts';
type Parsed = number[][][];
function cranmer(coeff: number[]): number {
const [x0, y0, c0, x1, y1, c1] = coeff;
// Cranmer's rule
const D = x0 * y1 - y0 * x1;
const Dx = c0 * y1 - y0 * c1;
const Dy = x0 * c1 - c0 * x1;
if ((Dx % D) || (Dy % D)) {
return 0;
}
const x = Dx / D;
const y = Dy / D;
return (3 * x) + y;
}
function part1(inp: Parsed): number {
return inp.reduce(
(t, [[x0, x1], [y0, y1], [c0, c1]]) =>
t + cranmer([x0, y0, c0, x1, y1, c1]),
0,
);
}
function part2(inp: Parsed): number {
return inp.reduce(
(t, [[x0, x1], [y0, y1], [c0, c1]]) =>
t + cranmer([x0, y0, c0 + 10000000000000, x1, y1, c1 + 10000000000000]),
0,
);
}
export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
return [part1(inp), part2(inp)];
}