Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.

PART 2 because it's basically only marginally more complex than PART 1

import * as fs from 'fs'

const stacks: string[][] = [];
fs.readFileSync(process.argv[2], 'utf-8').split('\n').map(l => {
  if (l.includes('[')) {
    l.match(/.{1,4}/g)?.map((c, i) => 
      (c[1] !== ' ') && (stacks[i] ??= []).unshift(c[1])
    )
  }

  if(l[0] === 'm') {
    const instr = l.match(/\d+/g)?.map(m => Number(m));
    if (!instr) return;
    const tmp: string[] = [];
    for(let i = 0; i < instr[0]; i++) {
      const c = stacks[instr[1]-1].pop();
      if (c) tmp.push(c);
    }
    tmp.reverse().map(c => stacks[instr[2]-1].push(c))
  }
}, [0,0])

console.log(stacks.map(s => s.pop()).join(''));

also, do none of you guys know regex?

Jump in the discussion.

No email address required.



Now playing: Donkey Kong Country Theme (DKC).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.