From 132ab771e85c656b260ca5a20767b5313dfa0ac7 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sat, 7 Dec 2024 12:46:35 -0700 Subject: [PATCH] Cleanup --- day7.ts | 62 ++++++++++++++++++++++++++------------------------------- t | 9 +++++++++ 2 files changed, 37 insertions(+), 34 deletions(-) create mode 100644 t diff --git a/day7.ts b/day7.ts index 1b0f5fe..24aeff3 100644 --- a/day7.ts +++ b/day7.ts @@ -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(args); + for (const [_tot, operands] of inp) { + operands.reverse(); + } return [part1(inp), part2(inp)]; } diff --git a/t b/t new file mode 100644 index 0000000..fc6e099 --- /dev/null +++ b/t @@ -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