| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- const fs = require('fs');
- const readline = require('readline');
- const SCREEN_WIDTH = 40;
- function visible(line, reg, allLines) {
- const cursor = line.length;
- line += (cursor === reg -1 || cursor === reg || cursor === reg +1) ? '#' : ' ';
- if (cursor === SCREEN_WIDTH -1) {
- allLines.push(line);
- line = "";
- }
- return line;
- }
- function doColorize(lines, x, y, color) {
- if (!lines[x] || lines[x][y] !== true)
- return;
- lines[x][y] = color;
- doColorize(lines, x -1, y, color);
- doColorize(lines, x +1, y, color);
- doColorize(lines, x -1, y -1, color);
- doColorize(lines, x +1, y -1, color);
- doColorize(lines, x -1, y +1, color);
- doColorize(lines, x +1, y +1, color);
- doColorize(lines, x, y -1, color);
- doColorize(lines, x, y +1, color);
- }
- function colorize(lines) {
- const colors = [ '\033[31m', '\033[32m', '\033[33m', '\033[34m', '\033[35m', '\033[36m' ];
- let colorIndex = 0;
- for (let i =0; i < lines.length; ++i)
- for (let j =0; j < lines[i].length; ++j)
- if (lines[i][j] === true) {
- doColorize(lines, i, j, colors[colorIndex % colors.length] +'#');
- ++colorIndex;
- }
- }
- async function main() {
- let currentTick = 1;
- let register = 1;
- let sum = 0;
- let data = "";
- let allLines = [];
- for await (let line of readline.createInterface({ input: process.stdin })) {
- let tickInc = 1;
- let prevValue = register;
- if (!line || !line.length)
- continue;
- if (line.startsWith('addx ')) {
- data = visible(data, register, allLines);
- data = visible(data, register, allLines);
- register += parseInt(line.substring(5));
- tickInc = 2;
- } else {
- data = visible(data, register, allLines);
- }
- if ((20+currentTick+tickInc) %40 < tickInc) {
- let currentValue = (20+currentTick+tickInc) %40 === 0 ? register : prevValue;
- sum += ((tickInc+currentTick)-((tickInc+currentTick+20)%40)) * currentValue;
- }
- currentTick += tickInc;
- }
- allLines = allLines.map(i => (i.split('').map(x => x === '#')));
- colorize(allLines);
- allLines.forEach(i => console.log(i.map(i => i || ' ').join('')));
- console.log('\033[39msum: ', sum);
- };
- (main());
|