Here's a javascript solution. Toggle part2 to false or true depending on what part you're on.
part2
let fs = require("fs"), acc = 0, taken = [],part2=true; const check = (data,index,returnfunc=()=>{}) => { if (taken.indexOf(index) > -1) returnfunc(acc) else { taken.push(index) let dedata = data[index] if (!dedata) console.log(acc) else { switch (dedata.slice(0,3)) { case "nop": check(data,index+1,returnfunc) break; case "acc": acc = acc + Number(dedata.match(/(?<=.{3} ).+/i)[0]) check(data,index+1,returnfunc) break; case "jmp": check(data,index+Number(dedata.match(/(?<=.{3} ).+/i)[0]),returnfunc) break; }}}} const controller = data => { const returnfunc = val => {if (!part2) console.log(val)} if (part2) { for (let i in data) { let modifieddata = [...data]; if ((data[i].slice(0,3) != "acc")) { if (data[i].slice(0,3) == "nop") modifieddata[i] = "jmp " + data[i].slice(4,data[i].length) else modifieddata[i] = "nop " + data[i].slice(4,data[i].length) taken = [], acc = 0, check(modifieddata,0) }}} else check(data,0,returnfunc) } fs.readFile("input.txt","utf8", (err,data) => controller(data.split("\n")))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's a javascript solution. Toggle
part2
to false or true depending on what part you're on.