diff options
author | Marvin Borner | 2022-12-21 09:50:39 +0100 |
---|---|---|
committer | Marvin Borner | 2022-12-21 09:50:39 +0100 |
commit | 551d0b6018c054f891ffb3c3fb92a65e839c2d51 (patch) | |
tree | 8e5c14bb53c564edc74712a652b66aff257a8b1d /2022/21/solve.js | |
parent | da36b579a724833d166f1076f906adee817b2527 (diff) |
whoo
Diffstat (limited to '2022/21/solve.js')
-rw-r--r-- | 2022/21/solve.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/2022/21/solve.js b/2022/21/solve.js new file mode 100644 index 0000000..818e463 --- /dev/null +++ b/2022/21/solve.js @@ -0,0 +1,32 @@ +d=require("fs").readFileSync("input", "utf8").trimEnd().split("\n").map(s=>s.split(": ")).reduce((a,s)=>({...a,[s[0]]:s[1].split(/ ([+|\-|*|/]) /)}),{}) + +r=m=>isNaN(d[m])?eval(r(d[m][0])+d[m][1]+r(d[m][2])):d[m] + +function part1() +{ + return r("root") +} + +function part2() +{ + // small binary search + // i'm sure there are better ways.. but it works + d["root"][1] = "-" + low = 1e2 + high = 1e20 + while (low != high) { + mid = (low + high)/2 + d["humn"][0] = `${mid}` + score = 0 - r("root") + if (score < 0) low = mid + else if (score == 0) return mid + else high = mid + } + return 0 +} + +const tic = performance.now(); +console.log(part1()); +console.log(part2()); +const toc = performance.now(); +console.log("TIME: " + ((toc - tic) / 1000).toFixed(6) + " seconds"); |