Last active
December 8, 2017 14:00
-
-
Save incon/82ab708321e9d4c1a59ff91935c36d7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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