Преглед на файлове

Adding day 2 of advent of code and some of the builtins I used to create it

Matt Coles преди 9 години
родител
ревизия
2348db170d
променени са 3 файла, в които са добавени 62 реда и са изтрити 1 реда
  1. 27 0
      adventofcode/advent02-1.mc
  2. 29 0
      libjs/maths.js
  3. 6 1
      libjs/stdlib.js

+ 27 - 0
adventofcode/advent02-1.mc

@@ -0,0 +1,27 @@
1
+; Requires the input for Advent of Code(http://adventofcode.com/) Day 2
2
+(include str fs maths)
3
+(assign input (fs::readIn "advent.txt"))
4
+(assign i 0)
5
+(assign resultone "")
6
+(assign resulttwo "")
7
+(assign resultthree "")
8
+(assign result 0)
9
+(assign currNum 1)
10
+(repeat (str::length input)
11
+  (assign current (str::charAtIndex input i))
12
+  (if (eq current "x") 
13
+    (assign currNum (add currNum 1)) |
14
+    (if (eq current "\n") 
15
+      (assign currNum 1) 
16
+      (assign result (add result (add (add (multiply 2 (multiply resultone resulttwo)) (add (multiply 2 (multiply resulttwo resultthree)) (multiply 2 (multiply resultone resultthree)))) (maths::min (multiply resultone resulttwo) (multiply resulttwo resultthree) (multiply resultthree resultone)))))
17
+      (assign resultone "")
18
+      (assign resulttwo "")
19
+      (assign resultthree "") | 
20
+        (if (eq currNum 1) (assign resultone (str::concat resultone current)))
21
+        (if (eq currNum 2) (assign resulttwo (str::concat resulttwo current))) 
22
+        (if (eq currNum 3) (assign resultthree (str::concat resultthree current)))
23
+    )
24
+  )
25
+  (assign i (add i 1))
26
+)
27
+(log result)

+ 29 - 0
libjs/maths.js

@@ -0,0 +1,29 @@
1
+var fs = require("fs")
2
+
3
+const builtins = {
4
+  min: function() {
5
+    let argArr = [...arguments]
6
+    return {
7
+      value: Math.min.apply(null, argArr.map((arg) => arg.value))
8
+    }
9
+  }
10
+}
11
+
12
+const my_handler = {
13
+  get: function (target, prop) {
14
+    let methods = Object.keys(builtins)
15
+    if (methods.includes(prop)) {
16
+      return builtins[prop]
17
+    } else {
18
+      console.error("Undefined function call! No such function: ", prop)
19
+    }
20
+  }, 
21
+}
22
+
23
+const proxy_obj = new Proxy({}, my_handler)
24
+let _
25
+
26
+module.exports = function (self) {
27
+  _ = self
28
+  return proxy_obj
29
+}

+ 6 - 1
libjs/stdlib.js

@@ -20,7 +20,7 @@ const builtins = {
20 20
     }
21 21
   },
22 22
   log: function (ref) {
23
-    console.log(ref.value)
23
+    console.log(JSON.stringify(ref.value))
24 24
   },
25 25
   ref: function (refname) {
26 26
     return {
@@ -72,6 +72,11 @@ const builtins = {
72 72
     return {
73 73
       value: (arg1.value % arg2.value)
74 74
     }
75
+  },
76
+  multiply: function (arg1, arg2) {
77
+    return {
78
+      value: (arg1.value * arg2.value)
79
+    }
75 80
   }
76 81
 }
77 82