Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 7, 2024
1 parent 4143360 commit 132ab77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
62 changes: 28 additions & 34 deletions day7.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,51 @@
import { type MainArgs, parseFile } from './lib/utils.ts';

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

// 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;
function calc(left: number, operands: number[], concat = false): boolean {
if (!operands.length) {
return left === 0;
}
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);
if (concat) { // Part 2 adds concatenation operator
const sleft = String(left);
if (sleft.endsWith(sfirst)) {
if (calc(Number(sleft.slice(0, -sfirst.length)), rest, true) === 0) {
return 0;
}
const sfirst = String(first);
const prefix = sleft.slice(0, -sfirst.length)
if ((prefix + sfirst === sleft) && calc(Number(prefix), rest, concat)) {
return true;
}
}
if (calc(left - first, rest, concat) === 0) {
return 0;
};
if (calc(left / first, rest, concat) === 0) {
return 0;

const dleft = left / first;
if (Number.isInteger(dleft) && calc(dleft, rest, concat)) {
return true;
}
return left;

const mleft = left - first;
return (mleft >= 0) && calc(mleft, rest, concat);
}

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

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;
return inp.reduce(
(t, [tot, operands]) => t + (calc(tot, operands, true) ? tot : 0),
0
);
}

export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
for (const [_tot, operands] of inp) {
operands.reverse();
}
return [part1(inp), part2(inp)];
}
9 changes: 9 additions & 0 deletions t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20

0 comments on commit 132ab77

Please sign in to comment.