Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 13 #27

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions day13.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
machines = machine|.., "\n"|

machine = button button prize

button = "Button" _ [AB] ":" _ "X+" @num _ "," _ "Y+" _ @num "\n"
prize = "Prize:" _ "X=" @num _ "," _ "Y=" _ @num "\n"

num = n:$[0-9]+ { return parseInt(n, 10) }
_ = [ \t]*
38 changes: 38 additions & 0 deletions day13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)];
}
2 changes: 1 addition & 1 deletion inputs
56 changes: 56 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,62 @@ export function divmod<T extends number | bigint>(x: T, y: T): [T, T] {
return [Math.floor(q) as T, r];
}

export function abs<T extends number | bigint>(a: T): T {
if (typeof a === 'bigint') {
return (a < 0 ? -a : a) as T;
}
return Math.abs(a) as T;
}

export function sign<T extends number | bigint>(a: T): T {
if (typeof a === 'bigint') {
if (a === 0n) {
return 0n as T;
}
return ((a < 0n) ? -1n : 1n) as T;
}
return Math.sign(a) as T;
}

export function egcd<T extends number | bigint>(
a: T,
b: T,
): [gcd: T, a: T, b: T] {
const bi = typeof a === 'bigint';
let s0 = (bi ? 1n : 1) as T;
let s1 = (bi ? 0n : 0) as T;
let t0 = s1;
let t1 = s0;
const sa = sign(a);
const sb = sign(b);

// Needs to work for both 0 and 0n
// deno-lint-ignore eqeqeq
if (a == 0) {
return [b, s1, t1];
}
if (!bi) {
if ((isNaN(a as number) || isNaN(b as number))) {
return [NaN as T, NaN as T, NaN as T];
}
if ((!isFinite(a as number) || !isFinite(b as number))) {
return [Infinity as T, Infinity as T, Infinity as T];
}
}

a = abs(a);
b = abs(b);

// deno-lint-ignore eqeqeq
while (b != 0) {
const [q, r] = divmod(a, b);
[s0, s1] = [s1, (s0 - (q * s1)) as T];
[t0, t1] = [t1, (t0 - (q * t1)) as T];
[a, b] = [b, r];
}
return [a, sa * s0 as T, sb * t0 as T];
}

export function gcd<T extends number | bigint>(...n: T[]): T {
switch (n.length) {
case 0:
Expand Down
1 change: 1 addition & 0 deletions test/day13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [40069, 71493195288102];
Loading