day 8
This commit is contained in:
parent
7124a52027
commit
e9e4c7a9cc
1000
day08/input.txt
Normal file
1000
day08/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
13
day08/part1.js
Normal file
13
day08/part1.js
Normal file
@ -0,0 +1,13 @@
|
||||
var regs = {};
|
||||
var lines = require("fs").readFileSync("input.txt").toString().split("\n").filter((a)=>(a));
|
||||
getreg = (reg)=>((reg in regs) ? (regs[reg]) : (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]);
|
||||
}
|
||||
}
|
||||
console.log(Math.max.apply(null, Object.keys(regs).map((a)=>(regs[a]))));
|
17
day08/part2.js
Normal file
17
day08/part2.js
Normal file
@ -0,0 +1,17 @@
|
||||
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);
|
Loading…
x
Reference in New Issue
Block a user