advent-of-code

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

commit 7a4b8b331576881e4e16e21ba140f5894590bc89
parent 20c9963950516dc1108d5b81c37c2542ec490bc5
Author: finwo <finwo@pm.me>
Date:   Tue, 15 Nov 2022 16:37:34 +0100

Added 2015/03 solution

Diffstat:
A2015/day-03/index.js | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+), 0 deletions(-)

diff --git a/2015/day-03/index.js b/2015/day-03/index.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const through = require('through2'); + +let poss = (new Array(2)).fill(0).map(() => ({ x: 0, y: 0 })); +let had = [[1]]; +let cnt = 1; + +fs.createReadStream('input') + + // Convert to characters + .pipe(through(function(chunk, enc, cb) { + chunk = chunk.toString(); + for(let i = 0; i < chunk.length; i++) { + this.push(chunk.charAt(i)); + } + cb(); + })) + + // Walk through directions + .pipe(through(function(chunk, enc, cb) { + const c = chunk.toString(); + let m = true; + + // Perform part 1 move + const pos = poss.shift(); + switch(c) { + case '^': pos.y--; break; + case '<': pos.x--; break; + case '>': pos.x++; break; + case 'v': pos.y++; break; + default: m = false; break; + } + + if (m) { + // Current mover has to wait for it's turn next round + poss.push(pos); + } else { + // Bail if no move was made + poss.unshift(pos); + return cb(); + } + + // Ensure the entry exists in our array & count houses with at least 1 present + if (!had[pos.x]) had[pos.x] = []; + if (!had[pos.x][pos.y]) { + had[pos.x][pos.y] = 0; + cnt++; + } + + // Increase the counter on that house + had[pos.x][pos.y]++; + + cb(); + })) + + + .on('finish', () => { + console.log(`${poss.length} Santa` + (poss.length == 1 ? '' : 's') + ` visited ${cnt} houses`); + })