advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

index.js (1057B)


      1 const fs            = require('fs');
      2 const through       = require('through2');
      3 const line_for_line = require('../common/line-for-line');
      4 
      5 let sum = 0;
      6 
      7 const machine = {
      8   cycle: 1,
      9   X: 1,
     10 
     11   step() {
     12 
     13     // // Part 1
     14     // if ((this.cycle-20)%40 == 0) {
     15     //   process.stdout.write(`${sum} + (${this.cycle} * ${this.X}) = `);
     16     //   sum += this.cycle * this.X;
     17     //   process.stdout.write(`${sum}\n`);
     18     // }
     19 
     20     // Part 2
     21     process.stdout.write(Math.abs(this.X - ((this.cycle-1)%40)) < 2 ? '#' : '.');
     22     if (this.cycle % 40 == 0) process.stdout.write('\n');
     23 
     24     this.cycle+=1;
     25   },
     26 
     27   addx(val) {
     28     this.step();
     29     this.step();
     30     this.X+=val;
     31   },
     32 
     33   noop() {
     34     this.step();
     35   }
     36 };
     37 
     38 fs.createReadStream('input')
     39   .pipe(line_for_line())
     40 
     41   // Load grid
     42   .pipe(through(function(line, enc, cb) {
     43     line = line.toString();
     44     const [aOp, aArg] = line.split(' ');
     45     const       iArg  = parseInt(aArg);
     46 
     47     machine[aOp](iArg);
     48 
     49     cb();
     50   }))
     51 
     52   .on('finish', () => {
     53     console.log({ sum });
     54     console.log('finish');
     55   });