diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-11-01 11:29:22 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-11-01 11:29:47 +0000 |
commit | af730bedc07615e43ace5b9f5a02027609abb01c (patch) | |
tree | 1914219bea81697b43491c8cf74a75ec73ae17d1 | |
parent | c07e124b692621bb7548e1e794b2cab5b16d8ea6 (diff) | |
download | advent-of-code-af730bedc07615e43ace5b9f5a02027609abb01c.tar.gz advent-of-code-af730bedc07615e43ace5b9f5a02027609abb01c.tar.bz2 advent-of-code-af730bedc07615e43ace5b9f5a02027609abb01c.zip |
Finished puzzle 6 in JabbaScript for 2015
-rw-r--r-- | 2015/puzzle-6.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/2015/puzzle-6.js b/2015/puzzle-6.js new file mode 100644 index 0000000..62c0382 --- /dev/null +++ b/2015/puzzle-6.js @@ -0,0 +1,83 @@ +const fs = require('node:fs/promises'); + +let arr = [] + +function count_1() { + return arr.filter(x => x).length; +} + +function count_2() { + return arr.reduce((x, y) => x + y); +} + +async function read_file() { + const data = await fs.readFile("6-input", {encoding: 'utf8'}); + return data; +} + +function parse_line(line) { + let [type, ...rest] = line.split(" ") + let obj = {type: "", bottom: [], top: []}; + if (type === "toggle") { + obj.type = "toggle"; + } else { + obj.type = rest[0]; + rest = rest.slice(1); + } + obj.bottom = rest[0].split(",").map(x => parseInt(x)); + obj.top = rest[2].split(",").map(x => parseInt(x)); + return obj +} + +function execute_inst_1(inst) { + for (let i = inst.bottom[0]; i <= inst.top[0]; ++i) { + for (let j = inst.bottom[1]; j <= inst.top[1]; ++j) { + if (inst.type === "on") { + arr[(i * 1000) + j] = true; + } else if (inst.type === "off") { + arr[(i * 1000) + j] = false; + } else { + arr[(i * 1000) + j] = !arr[(i * 1000) + j]; + } + } + } +} + +function execute_inst_2(inst) { + for (let i = inst.bottom[0]; i <= inst.top[0]; ++i) { + for (let j = inst.bottom[1]; j <= inst.top[1]; ++j) { + if (inst.type === "on") { + arr[(i * 1000) + j] += 1; + } else if (inst.type === "off") { + arr[(i * 1000) + j] = Math.max(0, arr[(i * 1000) + j] - 1); + } else { + arr[(i * 1000) + j] += 2; + } + } + } +} + +(async () => { + let lines = (await read_file()).split(/\n/); + const insts = lines.map(parse_line); + + for (let i = 0; i < 1000000; ++i) { + arr.push(false); + } + for (let i = 0; i < insts.length; ++i) { + execute_inst_1(insts[i]); + } + console.log("Round 1:", count_1()); + + for (let i = 0; i < 1000000; ++i) { + arr[i] = 0; + } + for (let i = 0; i < insts.length; ++i) { + execute_inst_2(insts[i]); + } + console.log("Round 2:", count_2()); +})() + +// Local Variables: +// compile-command: "node puzzle-6.js" +// End: |