-
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
4 changed files
with
63 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lines = (@num ":" _ @num|1.., _|"\n")* | ||
|
||
num = n:$[0-9]+ { return parseInt(n, 10) } | ||
_ = [ \t]+ |
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,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)]; | ||
} |
Submodule inputs
updated
from 1a785f to 977e7c
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 @@ | ||
export default [1038838357795, 254136560217241]; |