diff options
author | Marvin Borner | 2023-12-19 12:26:39 +0100 |
---|---|---|
committer | Marvin Borner | 2023-12-19 12:26:39 +0100 |
commit | a3820a8523c6e75c68c5eb3320baab2756e45544 (patch) | |
tree | 1c1c0aa47997d784073469b1449a6754e7c6bddb /2023/19 | |
parent | 0b0b2a6878c569e36d91be4263d1a9ab1947cc54 (diff) |
evil part1
Diffstat (limited to '2023/19')
-rw-r--r-- | 2023/19/solve.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/2023/19/solve.js b/2023/19/solve.js new file mode 100644 index 0000000..4a50178 --- /dev/null +++ b/2023/19/solve.js @@ -0,0 +1,28 @@ +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(":"))]); +parts = parts.split("\n").map((l) => l.slice(1, -1)); + +const findWorkflow = (name) => workflows.filter(([n, _]) => name == n)[0][1]; + +let part1 = 0; +for (const part of parts) { + eval(part); + let workflow = "in"; + while (workflow != "A" && workflow != "R") { + const rules = findWorkflow(workflow); + for (const rule of rules) { + if (rule.length == 1) { + workflow = rule[0]; + break; + } else if (eval(rule[0]) == true) { + workflow = rule[1]; + break; + } + } + } + if (workflow == "A") part1 += x + m + a + s; +} +console.log(part1); + +let part2 = 0 |