This commit is contained in:
2022-12-05 18:17:03 +00:00
parent 6b2e5bbe2c
commit f214ed1fe3
7 changed files with 682 additions and 36 deletions

32
src/2022/day5/index.ts Normal file
View File

@@ -0,0 +1,32 @@
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);
}
}
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;