Last active
February 14, 2023 01:21
-
-
Save wise-introvert/32e87e14d95880870d88c86166089d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @desc Day 5: Supply Stacks | |
* Expedition can depart once final supplies have been unloaded from ships | |
* Supplies are stored in stacks of marked crates and need to be rearranged | |
* Ship has cargo crane to move crates between stacks in planned steps | |
* Elves have a drawing of the starting stacks and rearrangement procedure | |
* Elves want to know which crate will be on top of each stack after rearrangement | |
* In example, top crates are C in stack 1, M in stack 2, and Z in stack 3 | |
* Elves need to know which crate ends up on top of each stack | |
* | |
* @example | |
* [D] | |
* [N] [C] | |
* [Z] [M] [P] | |
* 1 2 3 | |
* | |
* move 1 from 2 to 1 | |
* move 3 from 1 to 3 | |
* move 2 from 2 to 1 | |
* move 1 from 1 to 2 | |
*/ | |
export const five = (input: string) => { | |
const steps: string = input.split(/\n\n/)[1]; | |
const stack: string = input.split(/\n\n/)[0]; | |
console.log("steps: ", steps); | |
const split: string[] = stack.replace(/\n/, " ").split(" "); | |
const length: number = stack | |
.split(/\n/) | |
.slice(-1)[0] | |
.trim() | |
.split(/\s/) | |
.filter((value: string): boolean => /\d/.test(value)).length; | |
let grid: string[][] = Array.from({ length }, () => []); | |
let result: string[] = []; | |
for (let i: number = 0; i < split.length; i++) { | |
const current: string = split[i]; | |
const next: string = split[i + 1]; | |
const parso: string = split[i + 2]; | |
if (!current) { | |
if (!next) { | |
if (!parso) { | |
result.push("-"); | |
i = i + 2; | |
} else { | |
result.push("*"); | |
i = i + 1; | |
} | |
} else { | |
result.push(" "); | |
} | |
} else { | |
result.push(current); | |
} | |
} | |
let a: string[] = []; | |
result.forEach((value: string): any => | |
/\n/.test(value) ? a.push(...value.split(/\n/)) : a.push(value) | |
); | |
a = a.filter( | |
(value: string): boolean => | |
// value.length > 0 && /[^\*\d \n]{1,}/gi.test(value) | |
value.length > 0 && /[^\*\d ]{1,}/gi.test(value) | |
); | |
Array.from({ length }).forEach((_, index: number): void => { | |
a.splice(0, 9).forEach((current: string, i: number): void => { | |
console.log(`Inserting ${current} in stack #${i + 1}...`); | |
grid[i].push(current); | |
}); | |
console.log("\n\n"); | |
}); | |
printGrid(grid); | |
steps | |
.trim() | |
.split(/\n/) | |
.forEach((step: string): void => { | |
step = step.replace(/(move|from|to)/gi, "").replace(/\s/, ""); | |
const variables: string[] = step | |
.split(" ") | |
.filter((value: string): boolean => value.length > 0); | |
console.log("variables: ", variables.join("_")); | |
const quantity: number = parseInt(variables[0]); | |
const from: number = parseInt(variables[1]); | |
const to: number = parseInt(variables[2]); | |
console.log(`moving ${quantity} boxes from ${from} to ${to}...`); | |
const valuesToMove: string[] = grid[from - 1].slice(0, quantity); | |
console.log( | |
`moving \n\t${valuesToMove.join(", ")}\nfrom\n\t${ | |
grid[from - 1] | |
}\nto\n\t${grid[to - 1]}...` | |
); | |
console.log(`boxes moved successfully!!\n`); | |
grid[to - 1].unshift(...grid[from - 1].splice(0, quantity)); | |
console.log("to: ", grid[to - 1]); | |
console.log("from: ", grid[from - 1]); | |
printGrid(grid); | |
console.log("===========================================\n\n"); | |
/* | |
grid[to - 1].unshift(...valuesToMove); | |
grid[from - 1] = grid[from - 1].splice( | |
0, | |
quantity + 1, | |
...Array.from({ length: quantity }, (): string => "-") | |
); | |
*/ | |
}); | |
console.log( | |
grid | |
.map((subarray: string[]): string[] => | |
subarray.filter((value: string): boolean => /\[\W\]/.test(value)) | |
) | |
.map(([first]) => first) | |
.join("") | |
); | |
console.log("grid: ", grid); | |
}; | |
function printGrid(grid: string[][]): void { | |
Array.from({ length: grid.length - 1 }).forEach((_, index: number): void => { | |
console.log( | |
grid.map((arr) => (arr[index] == "-" ? " - " : arr[index])).join(" ") | |
); | |
}); | |
console.log( | |
` ${Array.from( | |
{ length: grid.length }, | |
(_, index: number): number => index + 1 | |
).join(" ")}` | |
); | |
} | |
const input: string = ` | |
[S] [C] [Z] | |
[F] [J] [P] [T] [N] | |
[G] [H] [G] [Q] [G] [D] | |
[V] [V] [D] [G] [F] [D] [V] | |
[R] [B] [F] [N] [N] [Q] [L] [S] | |
[J] [M] [M] [P] [H] [V] [B] [B] [D] | |
[L] [P] [H] [D] [L] [F] [D] [J] [L] | |
[D] [T] [V] [M] [J] [N] [F] [M] [G] | |
1 2 3 4 5 6 7 8 9 | |
move 3 from 4 to 6 | |
move 1 from 5 to 8 | |
move 3 from 7 to 3 | |
move 4 from 5 to 7 | |
move 1 from 7 to 8 | |
move 3 from 9 to 4 | |
move 2 from 8 to 2 | |
move 4 from 4 to 5 | |
move 2 from 5 to 1 | |
move 2 from 5 to 6 | |
move 7 from 8 to 1 | |
move 9 from 3 to 9 | |
move 11 from 6 to 5 | |
move 2 from 6 to 7 | |
move 12 from 1 to 4 | |
move 10 from 2 to 9 | |
move 2 from 3 to 9 | |
move 1 from 7 to 5 | |
move 4 from 7 to 6 | |
move 2 from 6 to 1 | |
move 5 from 1 to 6 | |
move 10 from 9 to 1 | |
move 9 from 9 to 8 | |
move 13 from 4 to 3 | |
move 7 from 6 to 2 | |
move 2 from 8 to 5 | |
move 9 from 3 to 9 | |
move 8 from 9 to 8 | |
move 4 from 8 to 4 | |
move 1 from 7 to 5 | |
move 3 from 9 to 1 | |
move 7 from 2 to 1 | |
move 1 from 3 to 1 | |
move 1 from 3 to 6 | |
move 1 from 6 to 1 | |
move 2 from 3 to 6 | |
move 5 from 4 to 1 | |
move 1 from 6 to 1 | |
move 3 from 8 to 7 | |
move 8 from 8 to 4 | |
move 3 from 5 to 4 | |
move 1 from 6 to 7 | |
move 1 from 5 to 8 | |
move 4 from 5 to 2 | |
move 7 from 5 to 8 | |
move 3 from 2 to 7 | |
move 7 from 4 to 8 | |
move 11 from 8 to 4 | |
move 15 from 4 to 1 | |
move 25 from 1 to 6 | |
move 4 from 8 to 7 | |
move 1 from 2 to 4 | |
move 11 from 6 to 4 | |
move 12 from 6 to 3 | |
move 1 from 1 to 9 | |
move 1 from 9 to 8 | |
move 16 from 1 to 3 | |
move 1 from 8 to 7 | |
move 12 from 4 to 6 | |
move 9 from 6 to 5 | |
move 3 from 6 to 5 | |
move 6 from 7 to 5 | |
move 3 from 3 to 5 | |
move 2 from 6 to 3 | |
move 11 from 5 to 8 | |
move 2 from 8 to 3 | |
move 2 from 1 to 4 | |
move 7 from 3 to 1 | |
move 2 from 4 to 6 | |
move 2 from 6 to 2 | |
move 5 from 7 to 3 | |
move 1 from 1 to 6 | |
move 1 from 1 to 8 | |
move 2 from 2 to 5 | |
move 1 from 7 to 4 | |
move 1 from 1 to 2 | |
move 10 from 3 to 5 | |
move 11 from 3 to 6 | |
move 1 from 4 to 9 | |
move 1 from 9 to 4 | |
move 1 from 4 to 2 | |
move 2 from 5 to 9 | |
move 2 from 2 to 8 | |
move 2 from 1 to 6 | |
move 2 from 1 to 2 | |
move 2 from 3 to 6 | |
move 3 from 8 to 1 | |
move 3 from 1 to 4 | |
move 7 from 8 to 3 | |
move 2 from 9 to 5 | |
move 2 from 4 to 9 | |
move 7 from 5 to 6 | |
move 2 from 8 to 6 | |
move 1 from 4 to 8 | |
move 2 from 2 to 4 | |
move 21 from 6 to 3 | |
move 10 from 5 to 7 | |
move 7 from 7 to 6 | |
move 1 from 9 to 3 | |
move 1 from 4 to 9 | |
move 1 from 9 to 4 | |
move 1 from 8 to 4 | |
move 8 from 6 to 4 | |
move 1 from 4 to 5 | |
move 1 from 5 to 8 | |
move 4 from 3 to 6 | |
move 1 from 8 to 2 | |
move 1 from 4 to 2 | |
move 2 from 7 to 3 | |
move 2 from 2 to 7 | |
move 22 from 3 to 5 | |
move 4 from 6 to 2 | |
move 2 from 6 to 9 | |
move 7 from 3 to 9 | |
move 6 from 9 to 1 | |
move 18 from 5 to 3 | |
move 2 from 5 to 4 | |
move 20 from 3 to 5 | |
move 3 from 7 to 3 | |
move 5 from 1 to 2 | |
move 11 from 5 to 7 | |
move 1 from 1 to 7 | |
move 3 from 9 to 3 | |
move 16 from 5 to 8 | |
move 7 from 8 to 7 | |
move 1 from 9 to 2 | |
move 8 from 2 to 3 | |
move 2 from 2 to 4 | |
move 3 from 3 to 1 | |
move 9 from 3 to 8 | |
move 1 from 6 to 3 | |
move 9 from 7 to 3 | |
move 3 from 1 to 8 | |
move 1 from 7 to 9 | |
move 1 from 9 to 4 | |
move 1 from 7 to 5 | |
move 10 from 4 to 5 | |
move 2 from 4 to 2 | |
move 19 from 8 to 5 | |
move 1 from 8 to 3 | |
move 4 from 3 to 5 | |
move 2 from 4 to 8 | |
move 4 from 7 to 8 | |
move 4 from 3 to 9 | |
move 4 from 7 to 6 | |
move 2 from 2 to 5 | |
move 2 from 3 to 2 | |
move 6 from 8 to 7 | |
move 1 from 8 to 4 | |
move 2 from 6 to 4 | |
move 3 from 4 to 8 | |
move 3 from 9 to 2 | |
move 4 from 7 to 8 | |
move 28 from 5 to 8 | |
move 16 from 8 to 4 | |
move 11 from 8 to 4 | |
move 3 from 3 to 4 | |
move 7 from 5 to 8 | |
move 13 from 8 to 7 | |
move 1 from 5 to 6 | |
move 1 from 6 to 7 | |
move 1 from 9 to 2 | |
move 2 from 6 to 2 | |
move 12 from 4 to 9 | |
move 4 from 4 to 1 | |
move 2 from 9 to 8 | |
move 4 from 8 to 3 | |
move 3 from 4 to 5 | |
move 4 from 4 to 1 | |
move 4 from 4 to 7 | |
move 3 from 7 to 9 | |
move 5 from 9 to 7 | |
move 7 from 2 to 3 | |
move 1 from 5 to 7 | |
move 8 from 1 to 5 | |
move 1 from 2 to 4 | |
move 11 from 3 to 1 | |
move 10 from 5 to 3 | |
move 3 from 9 to 1 | |
move 3 from 9 to 6 | |
move 5 from 1 to 6 | |
move 7 from 6 to 9 | |
move 8 from 9 to 7 | |
move 9 from 3 to 4 | |
move 1 from 6 to 9 | |
move 8 from 7 to 1 | |
move 9 from 4 to 2 | |
move 2 from 1 to 6 | |
move 3 from 2 to 6 | |
move 4 from 4 to 6 | |
move 2 from 9 to 8 | |
move 2 from 1 to 2 | |
move 1 from 3 to 8 | |
move 2 from 8 to 4 | |
move 1 from 6 to 8 | |
move 11 from 1 to 6 | |
move 1 from 1 to 5 | |
move 3 from 2 to 9 | |
move 2 from 9 to 3 | |
move 1 from 1 to 7 | |
move 2 from 4 to 9 | |
move 4 from 2 to 9 | |
move 2 from 8 to 5 | |
move 10 from 6 to 1 | |
move 2 from 5 to 6 | |
move 5 from 9 to 8 | |
move 5 from 8 to 7 | |
move 1 from 2 to 1 | |
move 7 from 1 to 2 | |
move 2 from 9 to 4 | |
move 1 from 3 to 5 | |
move 15 from 7 to 2 | |
move 8 from 6 to 3 | |
move 2 from 4 to 3 | |
move 2 from 6 to 4 | |
move 4 from 7 to 1 | |
move 4 from 7 to 5 | |
move 1 from 6 to 4 | |
move 3 from 1 to 7 | |
move 5 from 7 to 6 | |
move 4 from 7 to 5 | |
move 18 from 2 to 4 | |
move 5 from 6 to 4 | |
move 4 from 1 to 2 | |
move 8 from 3 to 8 | |
move 2 from 8 to 4 | |
move 2 from 3 to 7 | |
move 1 from 5 to 7 | |
move 3 from 8 to 4 | |
move 2 from 7 to 2 | |
move 1 from 3 to 8 | |
move 9 from 2 to 6 | |
move 2 from 8 to 6 | |
move 1 from 7 to 3 | |
move 1 from 3 to 5 | |
move 3 from 6 to 8 | |
move 1 from 8 to 5 | |
move 1 from 5 to 9 | |
move 1 from 1 to 2 | |
move 5 from 4 to 6 | |
move 10 from 6 to 2 | |
move 5 from 2 to 6 | |
move 5 from 6 to 4 | |
move 1 from 6 to 3 | |
move 6 from 4 to 6 | |
move 3 from 2 to 6 | |
move 2 from 2 to 3 | |
move 11 from 4 to 6 | |
move 1 from 9 to 5 | |
move 4 from 6 to 7 | |
move 1 from 4 to 3 | |
move 12 from 4 to 3 | |
move 1 from 8 to 6 | |
move 9 from 5 to 7 | |
move 1 from 5 to 2 | |
move 1 from 8 to 5 | |
move 1 from 4 to 9 | |
move 9 from 7 to 9 | |
move 1 from 3 to 4 | |
move 2 from 3 to 6 | |
move 2 from 5 to 6 | |
move 2 from 8 to 5 | |
move 11 from 3 to 4 | |
move 2 from 3 to 1 | |
move 1 from 2 to 3 | |
move 1 from 3 to 8 | |
move 3 from 7 to 9 | |
move 5 from 4 to 2 | |
move 2 from 5 to 8 | |
move 6 from 4 to 2 | |
move 1 from 1 to 3 | |
move 12 from 9 to 1 | |
move 6 from 1 to 6 | |
move 1 from 8 to 4 | |
move 1 from 8 to 3 | |
move 5 from 2 to 7 | |
move 2 from 3 to 9 | |
move 5 from 7 to 1 | |
move 1 from 7 to 5 | |
move 2 from 9 to 1 | |
move 14 from 1 to 7 | |
move 2 from 4 to 7 | |
move 7 from 2 to 4 | |
move 1 from 2 to 1 | |
move 1 from 1 to 3 | |
move 1 from 5 to 4 | |
move 1 from 9 to 6 | |
move 16 from 6 to 5 | |
move 2 from 5 to 4 | |
move 12 from 6 to 8 | |
move 10 from 4 to 8 | |
move 9 from 7 to 3 | |
move 4 from 7 to 6 | |
move 11 from 5 to 8 | |
move 2 from 5 to 2 | |
move 14 from 8 to 9 | |
move 1 from 5 to 1 | |
move 3 from 9 to 4 | |
move 2 from 2 to 1 | |
move 7 from 8 to 3 | |
move 6 from 3 to 5 | |
move 8 from 9 to 8 | |
move 1 from 6 to 1 | |
move 1 from 4 to 2 | |
move 4 from 3 to 8 | |
move 1 from 7 to 2 | |
move 3 from 1 to 5 | |
move 2 from 5 to 7 | |
move 3 from 9 to 2 | |
move 1 from 1 to 8 | |
move 5 from 5 to 4 | |
move 2 from 7 to 8 | |
move 4 from 2 to 5 | |
move 1 from 2 to 4 | |
move 2 from 7 to 8 | |
move 4 from 6 to 2 | |
move 6 from 5 to 3 | |
move 1 from 6 to 5 | |
move 1 from 5 to 3 | |
move 1 from 3 to 8 | |
move 8 from 8 to 3 | |
move 9 from 8 to 5 | |
move 9 from 8 to 2 | |
move 2 from 8 to 9 | |
move 2 from 3 to 8 | |
move 5 from 5 to 8 | |
move 1 from 3 to 7 | |
move 2 from 9 to 5 | |
move 7 from 2 to 4 | |
move 14 from 4 to 6 | |
move 2 from 2 to 7 | |
move 1 from 7 to 3 | |
move 1 from 7 to 9 | |
move 3 from 5 to 2 | |
move 1 from 7 to 1 | |
move 3 from 2 to 4 | |
move 7 from 8 to 2 | |
move 3 from 6 to 1 | |
move 17 from 3 to 1 | |
move 2 from 8 to 3 | |
move 6 from 2 to 7 | |
move 2 from 7 to 9 | |
move 3 from 6 to 8 | |
move 2 from 8 to 6 | |
move 4 from 2 to 1 | |
move 3 from 4 to 7 | |
move 1 from 8 to 7 | |
move 1 from 8 to 9 | |
move 1 from 4 to 2 | |
move 3 from 5 to 7 | |
move 2 from 3 to 1 | |
move 2 from 3 to 5 | |
move 5 from 7 to 4 | |
move 5 from 7 to 3 | |
move 1 from 4 to 8 | |
move 3 from 3 to 1 | |
move 6 from 1 to 3 | |
move 1 from 7 to 5 | |
move 2 from 9 to 2 | |
move 3 from 5 to 8 | |
move 1 from 8 to 1 | |
move 8 from 3 to 5 | |
move 1 from 4 to 9 | |
move 3 from 6 to 5 | |
move 3 from 6 to 3 | |
move 2 from 3 to 7 | |
move 1 from 4 to 7 | |
move 3 from 6 to 4 | |
move 2 from 7 to 2 | |
move 1 from 7 to 8 | |
move 2 from 5 to 4 | |
move 1 from 6 to 1 | |
move 7 from 4 to 7 | |
move 7 from 5 to 2 | |
move 10 from 2 to 3 | |
move 3 from 2 to 6 | |
move 3 from 8 to 1 | |
move 1 from 8 to 7 | |
move 2 from 6 to 3 | |
move 1 from 6 to 9 | |
move 4 from 7 to 5 | |
move 16 from 1 to 5 | |
move 1 from 9 to 7 | |
move 3 from 7 to 6 | |
move 11 from 5 to 6 | |
move 2 from 7 to 9 | |
move 12 from 6 to 4 | |
move 2 from 6 to 9 | |
move 6 from 3 to 2 | |
move 1 from 5 to 7 | |
move 5 from 9 to 5 | |
move 1 from 9 to 6 | |
move 4 from 3 to 7 | |
move 1 from 4 to 2 | |
move 7 from 2 to 5 | |
move 3 from 5 to 2 | |
move 6 from 5 to 6 | |
move 3 from 2 to 6 | |
move 9 from 6 to 8 | |
move 5 from 5 to 9 | |
move 5 from 7 to 1 | |
move 4 from 1 to 9 | |
move 2 from 9 to 4 | |
move 1 from 6 to 7 | |
move 9 from 4 to 1 | |
move 7 from 5 to 9 | |
move 18 from 1 to 3 | |
move 9 from 9 to 5 | |
move 8 from 8 to 2 | |
move 1 from 2 to 5 | |
move 4 from 2 to 3 | |
move 4 from 9 to 6 | |
move 1 from 4 to 8 | |
move 2 from 5 to 7 | |
move 2 from 9 to 2 | |
move 10 from 3 to 9 | |
move 5 from 5 to 9 | |
move 1 from 7 to 2 | |
move 2 from 8 to 7 | |
move 2 from 3 to 5 | |
move 2 from 9 to 1 | |
move 2 from 7 to 3 | |
move 1 from 2 to 1 | |
move 5 from 5 to 8 | |
move 1 from 2 to 1 | |
move 15 from 3 to 6 | |
move 1 from 7 to 6 | |
move 10 from 6 to 5 | |
move 1 from 7 to 8 | |
move 4 from 1 to 6 | |
move 1 from 8 to 3 | |
move 2 from 1 to 5 | |
move 3 from 8 to 1 | |
move 1 from 4 to 6 | |
move 1 from 4 to 2 | |
move 4 from 9 to 7 | |
move 6 from 5 to 7 | |
move 3 from 1 to 9 | |
move 10 from 6 to 8 | |
move 2 from 1 to 3 | |
move 8 from 7 to 9 | |
move 1 from 9 to 6 | |
move 2 from 7 to 9 | |
move 3 from 3 to 5 | |
move 1 from 2 to 6 | |
move 2 from 6 to 5 | |
move 5 from 9 to 4 | |
move 4 from 8 to 2 | |
move 1 from 1 to 3 | |
move 4 from 5 to 9 | |
move 3 from 6 to 1 | |
move 2 from 1 to 5 | |
move 3 from 5 to 2 | |
move 8 from 8 to 3 | |
move 11 from 9 to 4 | |
move 13 from 4 to 8 | |
move 2 from 9 to 2 | |
move 2 from 3 to 1 | |
move 1 from 4 to 1 | |
move 1 from 3 to 8 | |
move 2 from 6 to 9 | |
move 7 from 8 to 1 | |
move 3 from 2 to 5 | |
move 7 from 2 to 5 | |
move 3 from 4 to 6 | |
move 4 from 9 to 2 | |
move 2 from 3 to 5 | |
move 9 from 5 to 6 | |
move 5 from 2 to 7 | |
move 2 from 9 to 2 | |
move 2 from 9 to 7 | |
move 12 from 6 to 8 | |
move 5 from 5 to 7 | |
move 1 from 9 to 8 | |
move 3 from 1 to 6 | |
move 5 from 5 to 8 | |
move 6 from 1 to 9 | |
move 2 from 1 to 5 | |
move 1 from 6 to 9 | |
move 5 from 9 to 7 | |
move 2 from 5 to 8 | |
move 11 from 7 to 6 | |
move 20 from 8 to 1 | |
move 2 from 9 to 8 | |
move 4 from 7 to 6 | |
move 6 from 8 to 3 | |
move 13 from 6 to 9 | |
move 4 from 3 to 2 | |
move 4 from 6 to 3 | |
move 2 from 3 to 6 | |
move 5 from 9 to 8 | |
move 2 from 7 to 1 | |
move 2 from 6 to 9 | |
move 6 from 8 to 3 | |
move 6 from 3 to 6 | |
move 5 from 2 to 9 | |
move 22 from 1 to 3 | |
move 3 from 2 to 1 | |
move 5 from 9 to 3 | |
move 1 from 1 to 6 | |
move 3 from 6 to 2 | |
move 1 from 2 to 4 | |
move 33 from 3 to 5 | |
move 1 from 8 to 7 | |
`; | |
console.log(five(input)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment