Created
December 7, 2024 05:12
-
-
Save bluepichu/1beee6e8ce79f225a9cd64a170fbe770 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Advent, f, fm, chr, ord } from "advent"; | |
import { Set, Map } from "immutable"; | |
const { compute, computeCheck } = await Advent({ day: 7 }); | |
compute(1, async (input) => { | |
const data = input.parse(f.nl(f.match`${fm.int().as("ans")}: ${fm.list(fm.int(), " ").as("deps")}`)); | |
let res = 0; | |
for (const { ans, deps } of data) { | |
if (ok(ans, deps)) { | |
res += ans; | |
} | |
} | |
return res; | |
}); | |
function ok(ans: number, deps: number[]): boolean { | |
if (deps.length === 1) { | |
return ans === deps[0]; | |
} | |
return ( | |
ok(ans, [deps[0] + deps[1], ...deps.slice(2)]) | |
|| ok(ans, [deps[0] * deps[1], ...deps.slice(2)]) | |
|| ok(ans, [concat(deps[0], deps[1]), ...deps.slice(2)]) | |
); | |
} | |
function concat(a: number, b: number) { | |
const len = b.toString().length; | |
return a * Math.pow(10, len) + b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment