84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
const sample = await Deno.readTextFile("sample.txt");
|
|
const input = await Deno.readTextFile("input.txt");
|
|
|
|
const parseStack = (stackData: string): string[][] => {
|
|
const rotateMatrix = (matrix: string[][]): string[][] =>
|
|
matrix[0].map((_, index) =>
|
|
matrix
|
|
.map((row2) => row2[index].trim())
|
|
.reverse()
|
|
.filter(Boolean)
|
|
);
|
|
|
|
return rotateMatrix(
|
|
stackData
|
|
.split("\n")
|
|
// Filter out last line and empty lines
|
|
.filter((line, index, arr) => Boolean(line) && index < arr.length - 1)
|
|
.map((line) =>
|
|
line
|
|
.split("")
|
|
.reduce((r, v, i) => {
|
|
const p = Math.floor(i / 4);
|
|
r[p] = r[p] || [];
|
|
r[p].push(v);
|
|
return r;
|
|
}, [] as string[][])
|
|
.map((row) => row[1])
|
|
)
|
|
);
|
|
};
|
|
|
|
const parseMoves = (
|
|
moveData: string
|
|
): { amount: number; from: number; to: number }[] => {
|
|
return moveData
|
|
.split("\n")
|
|
.filter(Boolean)
|
|
.map(
|
|
(line): number[] =>
|
|
line
|
|
.match(/move\ (\d+)\ from\ (\d+)\ to\ (\d+)/)
|
|
?.slice(1)
|
|
.map((v) => parseInt(v, 10)) || []
|
|
)
|
|
.map(([amount, from, to]) => ({ amount, from: from - 1, to: to - 1 }));
|
|
};
|
|
|
|
const solvePart1 = (data: string): string => {
|
|
const [stackData, moveData] = data.split("\n\n");
|
|
const stacks = parseStack(stackData);
|
|
const moves = parseMoves(moveData);
|
|
return moves
|
|
.reduce((stack, move) => {
|
|
const length = stack[move.from].length;
|
|
stack[move.to] = stack[move.to].concat(
|
|
stack[move.from].splice(length - move.amount, length).reverse()
|
|
);
|
|
return stack;
|
|
}, stacks)
|
|
.map((stack) => stack.pop())
|
|
.join("");
|
|
};
|
|
|
|
console.log("Sample:", solvePart1(sample));
|
|
console.log("Input", solvePart1(input));
|
|
|
|
const solvePart2 = (data: string): string => {
|
|
const [stackData, moveData] = data.split("\n\n");
|
|
const stacks = parseStack(stackData);
|
|
const moves = parseMoves(moveData);
|
|
return moves
|
|
.reduce((stack, move) => {
|
|
const length = stack[move.from].length;
|
|
stack[move.to] = stack[move.to].concat(
|
|
stack[move.from].splice(length - move.amount, length)
|
|
);
|
|
return stack;
|
|
}, stacks)
|
|
.map((stack) => stack.pop())
|
|
.join("");
|
|
};
|
|
console.log("Sample:", solvePart2(sample));
|
|
console.log("Input", solvePart2(input));
|