advent-of-code-2022/day05/index.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-12-05 20:26:45 +00:00
import { readFileSync } from "fs";
const input = readFileSync("stacks.txt", "utf-8");
const stacks = [];
for(let i = 0; i < 9; i++) stacks[i] = [];
const offset = 4;
let isStackInsertingDone = false, tmp;
input.split('\n').forEach(line => {
if(line === '') {
isStackInsertingDone = true;
for(let i = 0; i < 9; i++) {
stacks[i].pop();
tmp = "";
while(stacks[i].length > 0) tmp += stacks[i].pop();
for(let j = 0; j < tmp.length; j++) stacks[i].push(tmp.charAt(j));
}
} else if(!isStackInsertingDone) {
for(let i = 0; i < 9; i++) {
const char = line.charAt(1 + (i * offset));
if(char !== ' ') stacks[i].push(char);
}
} else {
const splitLine = line.split(' ');
const howMany = parseInt(splitLine[1]), from = parseInt(splitLine[3]) - 1, to = parseInt(splitLine[5]) - 1;
2022-12-05 20:32:22 +00:00
const tempStack = [];
for(let i = 0; i < howMany; i++) {
tempStack.push(stacks[from].pop());
}
2022-12-05 20:26:45 +00:00
for(let i = 0; i < howMany; i++) {
2022-12-05 20:32:22 +00:00
stacks[to].push(tempStack.pop())
2022-12-05 20:26:45 +00:00
}
}
});
let out = "";
for(let i = 0; i < 9; i++) out += stacks[i].pop();
console.log(out);