index.js (1232B)
1 const fs = require('fs'); 2 const through = require('through2'); 3 const line_for_line = require('../common/line-for-line'); 4 5 function rotate(matrix) { 6 const out = []; 7 for(let x=0; x<matrix.length; x++) { 8 for(let y=0; y<matrix[x].length; y++) { 9 const k = matrix.length - 1 - x; 10 out[y] = out[y] || []; 11 out[y][k] = matrix[x][y]; 12 } 13 } 14 return out; 15 } 16 17 const stacks = rotate( 18 fs 19 .readFileSync('stacks', 'utf-8') 20 .split('\n') 21 .map(row => row.split('')) 22 ) 23 .map(row => row.filter(x => x.replace(' ', ''))); 24 25 fs.createReadStream('input') 26 .pipe(line_for_line()) 27 28 // Business logic 29 .pipe(through(function(line, enc, cb) { 30 line = line.toString(); 31 32 const [,amount,,src,,dst] = line.split(' ').map(x => parseInt(x)); 33 34 let tmp = []; 35 for(let i=0; i<amount; i++) { 36 console.log({dst}); 37 tmp.push(stacks[src-1].pop()); 38 } 39 // stacks[dst-1].push(...tmp); 40 stacks[dst-1].push(...(tmp.reverse())); 41 42 43 console.log({line, amount, src, dst}); 44 45 cb(); 46 })) 47 48 .on('finish', () => { 49 // done 50 let tops = ''; 51 for(let i=0; i<stacks.length; i++) { 52 tops+=stacks[i][stacks[i].length-1]; 53 } 54 55 console.log({stacks, tops}); 56 });