summaryrefslogtreecommitdiffstats
path: root/day18/day18-1.js
diff options
context:
space:
mode:
Diffstat (limited to 'day18/day18-1.js')
-rw-r--r--day18/day18-1.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/day18/day18-1.js b/day18/day18-1.js
new file mode 100644
index 0000000..992da27
--- /dev/null
+++ b/day18/day18-1.js
@@ -0,0 +1,22 @@
+const data = require('./data')
+const lines = data.split(/\n/g).filter(Boolean)
+
+const ev = (str) => {
+ let spl = str.split(' ')
+ while(spl.length > 1) {
+ spl = [eval(spl.slice(0, 3).join(''))].concat(spl.slice(3))
+ }
+ return spl[0]
+}
+
+const evPar = (str) => {
+ while(/\(/.test(str)) {
+ str = str.replace(/\(([^()]+)\)/g, (match, par) => ev(par))
+ }
+ return ev(str)
+}
+
+let sum = 0
+lines.forEach(l => sum += evPar(l))
+console.log('Answer: ', sum)
+