Files
AoC-2021/src/2022/day5/index.ts
2022-12-05 21:05:25 +00:00

47 lines
1.6 KiB
TypeScript

import path from 'path';
import { readFile, sum } from "../../utils";
let input = readFile(path.resolve(__dirname, 'input.txt'));
let start, elapsed;
start = process.hrtime.bigint();
const [stack, rules] = input.split('\n\n').map(entry => entry.split('\n'))
let stackInput = stack[stack.length - 1].split(' ').map(idx => idx.split(" ").join("")).map(idx => []);
const indexes = stackInput.map((arr, idx) => stack[stack.length - 1].indexOf(idx + 1).toString()).map(Number);
for (let idx = stack.length - 2; idx >= 0; idx--) {
for (let idx2 = 0; idx2 < indexes.length; idx2++) {
const char = stack[idx][indexes[idx2]];
if (char !== ' ') stackInput[idx2].push(char);
}
}
let stackInputPart2: any[] = JSON.parse(JSON.stringify(stackInput));
for (let idx = 0; idx < rules.length; idx++) {
const [move, from, to] = rules[idx].split(' ').filter(el => !isNaN(Number(el))).map(Number);
for (let num = 0; num < move; num++) stackInput[to - 1].push(stackInput[from - 1].pop());
}
const part1 = stackInput.map(buck => buck[buck.length - 1]).join("");
console.log("Part1:", part1);
elapsed = process.hrtime.bigint() - start;
for (let idx = 0; idx < rules.length; idx++) {
const [move, from, to] = rules[idx].split(' ').filter(el => !isNaN(Number(el))).map(Number);
let cratemover9001: any[] = [];
for (let num = 0; num < move; num++) cratemover9001.push(stackInputPart2[from - 1].pop());
stackInputPart2[to - 1] = stackInputPart2[to - 1].concat(cratemover9001.reverse());
}
const part2 = stackInputPart2.map(buck => buck[buck.length - 1]).join("");
console.log("Part2:", part2);