aboutsummaryrefslogtreecommitdiff
path: root/2023/19
diff options
context:
space:
mode:
authorMarvin Borner2023-12-19 14:29:24 +0100
committerMarvin Borner2023-12-19 14:34:55 +0100
commit05c842e23abe76e33e23fb4a0f1d71fcdf3d1db6 (patch)
tree4999fb2e0093897d659c669f08b88dfd4b745c0c /2023/19
parenta3820a8523c6e75c68c5eb3320baab2756e45544 (diff)
wtf
Diffstat (limited to '2023/19')
-rw-r--r--2023/19/solve.js37
1 files changed, 35 insertions, 2 deletions
diff --git a/2023/19/solve.js b/2023/19/solve.js
index 4a50178..829e41f 100644
--- a/2023/19/solve.js
+++ b/2023/19/solve.js
@@ -1,6 +1,6 @@
const fs = require("fs");
let [workflows, parts] = fs.readFileSync("input", "utf8").trim().split("\n\n");
-workflows = workflows.split("\n").map((l) => [l.split("{")[0], l .split("{")[1] .slice(0, -1).split(",").map((w) => w.split(":"))]);
+workflows = workflows.split("\n").map((l) => [l.split("{")[0], l.split("{")[1].slice(0, -1).split(",").map((w) => w.split(":"))]);
parts = parts.split("\n").map((l) => l.slice(1, -1));
const findWorkflow = (name) => workflows.filter(([n, _]) => name == n)[0][1];
@@ -25,4 +25,37 @@ for (const part of parts) {
}
console.log(part1);
-let part2 = 0
+const rec = (name, ranges) => {
+ if (name == "R") return 0;
+ if (name == "A") return Object.values(ranges).map(([lo, hi]) => Math.max(0, hi - lo - 1)).reduce((a, b) => a * b, 1);
+
+ let res = 0;
+ const rules = findWorkflow(name);
+ for (const rule of rules) {
+ if (rule.length == 1) {
+ res += rec(rule[0], ranges);
+ } else {
+ const [v, cmp, ..._n] = rule[0];
+ const n = +_n.join("");
+
+ let aah = structuredClone(ranges);
+ if (cmp == ">") {
+ const huh = Math.max(aah[v][0], n);
+ aah[v][0] = huh;
+ res += rec(rule[1], aah);
+ ranges[v][1] = huh + 1;
+ } else if (cmp == "<") {
+ const huh = Math.min(aah[v][1], n);
+ aah[v][1] = huh;
+ res += rec(rule[1], aah);
+ ranges[v][0] = huh - 1;
+ }
+ }
+ }
+
+ return res;
+};
+
+console.log(
+ rec("in", { x: [0, 4001], m: [0, 4001], a: [0, 4001], s: [0, 4001] }),
+);