var input = require("fs").readFileSync("input.txt").toString(); var nogarbage = input.replace(/!./g, "").replace(/<[^>]*>/g, ""); var json = nogarbage.replace(/{/g, "[").replace(/}/g, "]").replace(/\[[,]+/g, "[").replace(/[,]+\]/g, "]"); var structure = JSON.parse(json); score = (o, d)=>(o.map((c)=>(score(c, d+1))).reduce((a, b)=>(a+b), 0) + d); console.log(score(structure, 1));