aboutsummaryrefslogtreecommitdiff
path: root/2022/21-Monkey-Math/first.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2022/21-Monkey-Math/first.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/2022/21-Monkey-Math/first.js b/2022/21-Monkey-Math/first.js
new file mode 100644
index 0000000..b6da414
--- /dev/null
+++ b/2022/21-Monkey-Math/first.js
@@ -0,0 +1,45 @@
+import * as fs from "fs";
+
+const simple = /(\w+): (\d+)/;
+const operation = /(\w+): (\w+) ([+\-*/]) (\w+)/;
+
+function load(filename) {
+ let res = {};
+ fs.readFileSync(filename, "utf8")
+ .trim()
+ .split("\n")
+ .forEach(line => {
+ const s = line.match(simple);
+ if (s) {
+ res[s[1]] = function() { return parseInt(s[2]); };
+ } else {
+ const o = line.match(operation);
+ res[o[1]] = function () {
+ let left = res[o[2]]();
+ let right = res[o[4]]();
+ switch(o[3]) {
+ case "+": return left + right;
+ case "-": return left - right;
+ case "*": return left * right;
+ case "/": return left / right;
+ }
+ };
+ }
+ });
+ return res;
+}
+
+let example = load("example");
+let input = load("input");
+
+function solve(input) {
+ return input["root"]();
+}
+
+const exampleOutput = solve(example);
+if (exampleOutput !== 152) {
+ console.log("Example failed with " + exampleOutput);
+ process.exit(1); // eslint-disable-line
+}
+
+console.log(solve(input));