Skip to content

Instantly share code, notes, and snippets.

@incon incon/AoC-2017-day8.js
Last active Dec 8, 2017

Embed
What would you like to do?
const data = require("fs").readFileSync("day8-input.txt", "UTF-8");
map = {};
maxCPU = 0;
data.split("\n").forEach(row => {
action = row.split(" if ")[0].split(" ");
conditionStr = row.split(" if ")[1];
conditionVar = conditionStr.split(" ")[0];
actionVar = action[0];
increment = action[1] === "inc";
actionVal = parseInt(action[2]);
!map.hasOwnProperty(actionVar) && (map[actionVar] = 0);
!map.hasOwnProperty(conditionVar) && (map[conditionVar] = 0);
condition = eval("map." + conditionStr);
if (condition) {
if (increment) {
map[actionVar] += actionVal;
if (map[actionVar] > maxCPU) {
maxCPU = map[actionVar];
}
} else {
map[actionVar] -= actionVal;
}
}
});
max = Math.max(...Object.values(map));
console.log("Part 1:", max);
console.log("Part 2:", maxCPU);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.