adventofcode-2017/day08/part2.js

18 lines
624 B
JavaScript
Raw Normal View History

2017-12-08 09:29:31 +00:00
var regs = {};
var lines = require("fs").readFileSync("input.txt").toString().split("\n").filter((a)=>(a));
getreg = (reg)=>((reg in regs) ? (regs[reg]) : (0));
var max = 0;
for(var line of lines){
var condition = new RegExp("[a-z]+ (inc|dec) (-?)[0-9]+ if (.*)").exec(line)[3].replace(/^([a-z]+) (.*)/, "getreg('$1') $2");
if(eval(condition)){
if(!(line.split(" ")[0] in regs)){
regs[line.split(" ")[0]] = 0;
}
regs[line.split(" ")[0]] += ((line.split(" ")[1] == "inc") ? 1 : -1) * parseInt(line.split(" ")[2]);
if(regs[line.split(" ")[0]] > max){
max = regs[line.split(" ")[0]];
}
}
}
console.log(max);