Skip to content

Commit

Permalink
Day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 7, 2024
1 parent 642532a commit 7fd94b1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions day7.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lines = (@num ":" _ @num|1.., _|"\n")*

num = n:$[0-9]+ { return parseInt(n, 10) }
_ = [ \t]+
57 changes: 57 additions & 0 deletions day7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { type MainArgs, parseFile } from './lib/utils.ts';

type Parsed = [number, number[]][];

// Starting from the front of the reversed list (the endq), see if we can rule
// out any of the operations
function calc(left: number, operands: number[], concat = false): number {
if ((left <= 0) || (Math.floor(left) !== left) || !operands.length) {
return left;
}
const [first, ...rest] = operands;
if (concat) { // Part 2
// This is most likely to rule out an operand.
// 20 || 123 = 20123. The opposite op is 20123 -- 123, which fails if
// LHS doesn't end in RHS, then truncates.
const sfirst = String(first);
const sleft = String(left);
if (sleft.endsWith(sfirst)) {
if (calc(Number(sleft.slice(0, -sfirst.length)), rest, true) === 0) {
return 0;
}
}
}
if (calc(left - first, rest, concat) === 0) {
return 0;
};
if (calc(left / first, rest, concat) === 0) {
return 0;
}
return left;
}

function part1(inp: Parsed): number {
let sum = 0;
for (const [tot, operands] of inp) {
if (calc(tot, operands.reverse()) === 0) {
sum += tot;
}
}
return sum;
}

function part2(inp: Parsed): number {
let sum = 0;
for (const [tot, operands] of inp) {
// Already reversed
if (calc(tot, operands, true) === 0) {
sum += tot;
}
}
return sum;
}

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
1 change: 1 addition & 0 deletions test/day7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [1038838357795, 254136560217241];

0 comments on commit 7fd94b1

Please sign in to comment.