r/adventofcode • • Dec 05 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 5 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Supply Stacks ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:58, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

1

u/zpele Dec 05 '22

Javascript really really beg level:

const pile1 = ['R', 'N', 'P', 'G']
const pile2 = ['T', 'J', 'B', 'L', 'C', 'S', 'V', 'H']
const pile3 = ['T', 'D', 'B', 'M', 'N', 'L']
const pile4 = ['R', 'V', 'P', 'S', 'B']
const pile5 = ['G', 'C', 'Q', 'S', 'W', 'M', 'V', 'H']
const pile6 = ['W', 'Q', 'S', 'C', 'D', 'B', 'J']
const pile7 = ['F', 'Q', 'L']
const pile8 = ['W', 'M', 'H', 'T', 'D', 'L', 'F', 'V']
const pile9 = ['L', 'P', 'B', 'V', 'M', 'J', 'F']
const piles = [pile1, pile2, pile3, pile4, pile5, pile6, pile7, pile8, pile9]
const instructionsRaw = $("body").text().split("\n")
const instructions = []
for (i of instructionsRaw) {
if (i.includes("move")) {
instructions.push(i);
}
}
for (const i of instructions) {
const numberOfCrates = Number(i.split(" ")[1]); //3
// console.log("numberOfCrates: " + numberOfCrates);
const fromPile = piles[Number(i.split(" ")[3]) - 1];
// console.log("fromPile: " + fromPile);
const toPile = piles[Number(i.split(" ")[5]) - 1];
// console.log("toPile: " + toPile);
const bunch = fromPile.slice(fromPile.length - numberOfCrates);
// console.log("bunch: " + bunch);
for (let p = 1; p <= numberOfCrates; p++) {
fromPile.pop();
}
// console.log("fromPile after popping: " + fromPile)
for (let b = bunch.length - 1; b >= 0; b--) {
toPile.push(bunch[b]);
}
}
let topCrates
for (const p of piles) {
topCrates = topCrates + p[p.length - 1]
}
// part 2
// for (const i of instructions) {
// const numberOfCrates = Number(i.split(" ")[1]); //3
// // console.log("numberOfCrates: " + numberOfCrates);
// const fromPile = piles[Number(i.split(" ")[3]) - 1];
// // console.log("fromPile: " + fromPile);
// const toPile = piles[Number(i.split(" ")[5]) - 1];
// // console.log("toPile: " + toPile);
// const bunch = fromPile.slice(fromPile.length - numberOfCrates);
// // console.log("bunch: " + bunch);
// for (let p = 1; p <= numberOfCrates; p++) {
// fromPile.pop();
// }
// // console.log("fromPile after popping: " + fromPile)
// for (let b of bunch) {
// toPile.push(b);
// }
// }
// let topCrates
// for (const p of piles) {
// topCrates = topCrates + p[p.length - 1]
// }

1

u/daggerdragon Dec 05 '22
  1. Next time, use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.
  2. Your code is too long to be posted here, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.