Skip to content

Instantly share code, notes, and snippets.

@incon
Last active December 8, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incon/82ab708321e9d4c1a59ff91935c36d7d to your computer and use it in GitHub Desktop.
Save incon/82ab708321e9d4c1a59ff91935c36d7d to your computer and use it in GitHub Desktop.
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