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.

I'm not as embarrassed about this code as yesterday's I guess.

var lines = document.body.innerText.split("\n");
var stacks = [];

for (var j=0;j<lines.length;j++) {
    var line = lines[j];
    if (line.charAt(1) === "1") break;

    var r = /\[[A-Z]\]/g
    while (m = r.exec(line)) {
        var i = Math.floor(m.index/4);
        if (!Array.isArray(stacks[i])) stacks[i] = [];
        stacks[i].push(m[0].substring(1,2));
    }
}

stacks.map((s) => s.reverse());

function moveLine(line) {
    var m = line.match(/move ([0-9]+) from ([0-9]+) to ([0-9]+)/)
    if (m) {
        m = m.map((n) => Number(n));
        for (var i=0;i<m[1];i++) {
            var l = stacks[m[2]-1].pop();
            if (l) {
                stacks[m[3]-1].push(l);
            }
        }
    }
}

lines.map(moveLine);
Jump in the discussion.

No email address required.



Now playing: Aquaduct Assault (Tropical Freeze).mp3

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