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