advent-of-code

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

commit c839f6de1619258ebeffd960290afd4cbf10de4c
parent 47b7fe31661ae1a6be1ed86a54b539484994e8f5
Author: finwo <finwo@pm.me>
Date:   Mon,  5 Dec 2022 09:24:55 +0100

2022/05 solution

Diffstat:
A2022/day-05/index.js | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day-05/stacks | 8++++++++
2 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/2022/day-05/index.js b/2022/day-05/index.js @@ -0,0 +1,56 @@ +const fs = require('fs'); +const through = require('through2'); +const line_for_line = require('../common/line-for-line'); + +function rotate(matrix) { + const out = []; + for(let x=0; x<matrix.length; x++) { + for(let y=0; y<matrix[x].length; y++) { + const k = matrix.length - 1 - x; + out[y] = out[y] || []; + out[y][k] = matrix[x][y]; + } + } + return out; +} + +const stacks = rotate( + fs + .readFileSync('stacks', 'utf-8') + .split('\n') + .map(row => row.split('')) +) + .map(row => row.filter(x => x.replace(' ', ''))); + +fs.createReadStream('input') + .pipe(line_for_line()) + + // Business logic + .pipe(through(function(line, enc, cb) { + line = line.toString(); + + const [,amount,,src,,dst] = line.split(' ').map(x => parseInt(x)); + + let tmp = []; + for(let i=0; i<amount; i++) { + console.log({dst}); + tmp.push(stacks[src-1].pop()); + } + // stacks[dst-1].push(...tmp); + stacks[dst-1].push(...(tmp.reverse())); + + + console.log({line, amount, src, dst}); + + cb(); + })) + + .on('finish', () => { + // done + let tops = ''; + for(let i=0; i<stacks.length; i++) { + tops+=stacks[i][stacks[i].length-1]; + } + + console.log({stacks, tops}); + }); diff --git a/2022/day-05/stacks b/2022/day-05/stacks @@ -0,0 +1,8 @@ + J B T + ML QLR + GQ WSBL +D DT MGVP +T NNNDJGN +WHHSCNRWD +NPPWHHBNG +LCWCPTMZW