-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |