index.js (1247B)
1 const fs = require('fs'); 2 const through = require('through2'); 3 const char_for_char = require('../common/char-for-char'); 4 5 let poss = (new Array(2)).fill(0).map(() => ({ x: 0, y: 0 })); 6 let had = [[1]]; 7 let cnt = 1; 8 9 fs.createReadStream('input') 10 .pipe(char_for_char()) 11 12 // Walk through directions 13 .pipe(through(function(chunk, enc, cb) { 14 const c = chunk.toString(); 15 let m = true; 16 17 // Perform part 1 move 18 const pos = poss.shift(); 19 switch(c) { 20 case '^': pos.y--; break; 21 case '<': pos.x--; break; 22 case '>': pos.x++; break; 23 case 'v': pos.y++; break; 24 default: m = false; break; 25 } 26 27 if (m) { 28 // Current mover has to wait for it's turn next round 29 poss.push(pos); 30 } else { 31 // Bail if no move was made 32 poss.unshift(pos); 33 return cb(); 34 } 35 36 // Ensure the entry exists in our array & count houses with at least 1 present 37 if (!had[pos.x]) had[pos.x] = []; 38 if (!had[pos.x][pos.y]) { 39 had[pos.x][pos.y] = 0; 40 cnt++; 41 } 42 43 // Increase the counter on that house 44 had[pos.x][pos.y]++; 45 46 cb(); 47 })) 48 49 50 .on('finish', () => { 51 console.log(`${poss.length} Santa` + (poss.length == 1 ? '' : 's') + ` visited ${cnt} houses`); 52 })