Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const eol = require('os').EOL;
- const startTime = new Date().getTime();
- let part1 = part2 = 0;
- let input = fs.readFileSync(__dirname + "/data.txt", 'utf8').split(eol).map(a => a.split(" ")).reverse();
- let wait = false;
- let cycle = spritePos = 1;
- let cmd, val;
- let pixel = 0;
- let lines = '';
- while (input.length || wait) {
- if (!wait && input.length)[cmd, val] = input.pop();
- if (cycle % 40 == 20) {
- part1 += spritePos * cycle;
- }
- Math.abs(spritePos - pixel) <= 1 ? lines += "#" : lines += "."
- if (++pixel == 40) {
- pixel = 0;
- lines += "\n"
- }
- if (wait) {
- spritePos += +val;
- wait = false
- } else if (cmd == "addx") wait = true;
- cycle++;
- }
- console.log(lines);
- let time = new Date().getTime() - startTime
- console.log(`Part 1: ${part1}\nPart 2: ${part2}\nTimer: ${time} ms`);
Advertisement
Add Comment
Please, Sign In to add comment