From e005fb739fa6d8658848222892079b1a71b3a1a9 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 5 Dec 2022 11:16:31 -0500 Subject: [PATCH] part 1 --- 2022/05/input | 512 ++++++++++++++++++++++++++++++++++++++++++++++ 2022/05/part1.js | 67 ++++++ 2022/05/part2.js | 1 + 2022/05/puzzle.md | 78 +++++++ 2022/05/sample | 9 + 2022/05/test.js | 20 ++ 6 files changed, 687 insertions(+) create mode 100644 2022/05/input create mode 100644 2022/05/part1.js create mode 100644 2022/05/part2.js create mode 100644 2022/05/puzzle.md create mode 100644 2022/05/sample create mode 100644 2022/05/test.js diff --git a/2022/05/input b/2022/05/input new file mode 100644 index 0000000..fc8b6aa --- /dev/null +++ b/2022/05/input @@ -0,0 +1,512 @@ + [C] [Q] [V] + [D] [D] [S] [M] [Z] + [G] [P] [W] [M] [C] [G] + [F] [Z] [C] [D] [P] [S] [W] +[P] [L] [C] [V] [W] [W] [H] [L] +[G] [B] [V] [R] [L] [N] [G] [P] [F] +[R] [T] [S] [S] [S] [T] [D] [L] [P] +[N] [J] [M] [L] [P] [C] [H] [Z] [R] + 1 2 3 4 5 6 7 8 9 + +move 2 from 4 to 6 +move 4 from 5 to 3 +move 6 from 6 to 1 +move 4 from 1 to 4 +move 4 from 9 to 4 +move 7 from 2 to 4 +move 1 from 9 to 3 +move 1 from 2 to 6 +move 2 from 9 to 5 +move 2 from 6 to 8 +move 5 from 8 to 1 +move 2 from 6 to 9 +move 5 from 8 to 3 +move 1 from 5 to 4 +move 3 from 7 to 2 +move 10 from 4 to 7 +move 7 from 4 to 3 +move 1 from 4 to 7 +move 1 from 7 to 9 +move 1 from 2 to 3 +move 11 from 1 to 7 +move 12 from 3 to 7 +move 8 from 3 to 8 +move 29 from 7 to 2 +move 3 from 7 to 3 +move 3 from 9 to 2 +move 4 from 5 to 3 +move 7 from 3 to 5 +move 28 from 2 to 3 +move 1 from 7 to 5 +move 2 from 8 to 5 +move 2 from 4 to 1 +move 2 from 1 to 4 +move 1 from 7 to 6 +move 1 from 7 to 1 +move 3 from 2 to 8 +move 1 from 1 to 7 +move 9 from 5 to 3 +move 12 from 3 to 1 +move 1 from 4 to 3 +move 1 from 6 to 4 +move 3 from 2 to 9 +move 16 from 3 to 7 +move 2 from 9 to 6 +move 5 from 7 to 2 +move 1 from 9 to 7 +move 1 from 4 to 2 +move 13 from 7 to 2 +move 13 from 2 to 7 +move 12 from 7 to 8 +move 2 from 6 to 4 +move 16 from 8 to 1 +move 4 from 3 to 1 +move 3 from 3 to 2 +move 1 from 5 to 7 +move 1 from 5 to 3 +move 3 from 4 to 6 +move 19 from 1 to 3 +move 5 from 8 to 4 +move 6 from 3 to 2 +move 5 from 4 to 2 +move 1 from 7 to 4 +move 1 from 4 to 9 +move 3 from 6 to 7 +move 1 from 9 to 2 +move 16 from 2 to 4 +move 9 from 1 to 8 +move 10 from 4 to 2 +move 2 from 7 to 5 +move 5 from 8 to 4 +move 12 from 2 to 9 +move 2 from 7 to 4 +move 12 from 9 to 5 +move 11 from 5 to 6 +move 3 from 1 to 9 +move 1 from 5 to 7 +move 2 from 9 to 2 +move 10 from 3 to 2 +move 1 from 9 to 2 +move 2 from 8 to 9 +move 1 from 7 to 8 +move 1 from 8 to 4 +move 7 from 2 to 6 +move 1 from 1 to 5 +move 5 from 3 to 1 +move 1 from 5 to 1 +move 2 from 3 to 9 +move 2 from 1 to 6 +move 3 from 9 to 8 +move 14 from 6 to 1 +move 1 from 3 to 5 +move 5 from 4 to 6 +move 1 from 9 to 6 +move 7 from 6 to 9 +move 1 from 6 to 2 +move 8 from 1 to 4 +move 7 from 1 to 7 +move 10 from 2 to 1 +move 4 from 7 to 6 +move 10 from 4 to 6 +move 5 from 8 to 2 +move 1 from 5 to 9 +move 2 from 2 to 6 +move 2 from 4 to 7 +move 1 from 2 to 7 +move 5 from 9 to 2 +move 1 from 2 to 9 +move 14 from 6 to 8 +move 2 from 8 to 4 +move 1 from 2 to 6 +move 4 from 9 to 3 +move 2 from 6 to 8 +move 5 from 4 to 5 +move 5 from 8 to 3 +move 1 from 2 to 4 +move 3 from 7 to 1 +move 2 from 2 to 7 +move 1 from 4 to 7 +move 1 from 4 to 5 +move 1 from 2 to 8 +move 1 from 4 to 9 +move 8 from 8 to 2 +move 3 from 1 to 5 +move 7 from 2 to 9 +move 8 from 1 to 6 +move 6 from 7 to 2 +move 2 from 2 to 8 +move 5 from 1 to 8 +move 3 from 6 to 8 +move 4 from 3 to 6 +move 3 from 6 to 2 +move 8 from 9 to 2 +move 11 from 5 to 7 +move 12 from 2 to 6 +move 2 from 3 to 7 +move 12 from 7 to 2 +move 10 from 6 to 9 +move 1 from 7 to 1 +move 12 from 8 to 7 +move 2 from 3 to 2 +move 8 from 9 to 7 +move 6 from 2 to 5 +move 1 from 1 to 6 +move 3 from 2 to 6 +move 1 from 3 to 7 +move 5 from 5 to 3 +move 10 from 7 to 2 +move 2 from 3 to 7 +move 8 from 7 to 6 +move 20 from 2 to 8 +move 5 from 8 to 1 +move 5 from 8 to 6 +move 1 from 5 to 7 +move 1 from 1 to 4 +move 4 from 1 to 2 +move 1 from 9 to 6 +move 3 from 3 to 1 +move 4 from 7 to 5 +move 1 from 9 to 8 +move 11 from 8 to 7 +move 1 from 4 to 9 +move 2 from 7 to 5 +move 31 from 6 to 9 +move 4 from 2 to 3 +move 6 from 5 to 1 +move 4 from 1 to 2 +move 7 from 7 to 8 +move 1 from 7 to 6 +move 1 from 1 to 7 +move 24 from 9 to 4 +move 2 from 7 to 8 +move 2 from 9 to 2 +move 2 from 7 to 5 +move 2 from 5 to 9 +move 3 from 4 to 1 +move 20 from 4 to 2 +move 1 from 6 to 1 +move 16 from 2 to 1 +move 4 from 3 to 1 +move 1 from 4 to 8 +move 5 from 8 to 5 +move 5 from 8 to 1 +move 1 from 5 to 2 +move 3 from 5 to 6 +move 33 from 1 to 6 +move 6 from 9 to 4 +move 15 from 6 to 7 +move 6 from 4 to 3 +move 1 from 5 to 3 +move 7 from 3 to 9 +move 11 from 7 to 5 +move 10 from 5 to 8 +move 2 from 7 to 3 +move 5 from 8 to 9 +move 1 from 7 to 5 +move 1 from 5 to 8 +move 1 from 5 to 7 +move 2 from 3 to 8 +move 2 from 7 to 5 +move 2 from 8 to 7 +move 1 from 5 to 9 +move 1 from 7 to 6 +move 3 from 8 to 6 +move 22 from 6 to 9 +move 1 from 7 to 6 +move 27 from 9 to 4 +move 18 from 4 to 8 +move 5 from 4 to 1 +move 1 from 5 to 1 +move 3 from 6 to 3 +move 2 from 3 to 5 +move 2 from 5 to 2 +move 1 from 2 to 6 +move 1 from 6 to 3 +move 9 from 8 to 6 +move 3 from 9 to 8 +move 9 from 6 to 5 +move 1 from 6 to 9 +move 15 from 8 to 5 +move 1 from 3 to 4 +move 6 from 1 to 8 +move 1 from 3 to 7 +move 8 from 5 to 8 +move 2 from 5 to 6 +move 3 from 4 to 6 +move 1 from 7 to 6 +move 2 from 5 to 3 +move 5 from 5 to 1 +move 2 from 3 to 7 +move 1 from 8 to 1 +move 10 from 2 to 9 +move 5 from 6 to 3 +move 7 from 8 to 5 +move 4 from 3 to 5 +move 1 from 2 to 1 +move 2 from 7 to 6 +move 5 from 1 to 5 +move 1 from 3 to 7 +move 1 from 7 to 6 +move 3 from 8 to 5 +move 4 from 6 to 4 +move 1 from 2 to 9 +move 5 from 4 to 6 +move 21 from 5 to 3 +move 2 from 8 to 4 +move 3 from 4 to 1 +move 1 from 8 to 4 +move 18 from 3 to 5 +move 2 from 3 to 6 +move 2 from 6 to 9 +move 2 from 6 to 2 +move 1 from 2 to 9 +move 19 from 9 to 4 +move 3 from 6 to 3 +move 2 from 9 to 4 +move 1 from 1 to 2 +move 1 from 3 to 7 +move 16 from 5 to 2 +move 4 from 1 to 9 +move 3 from 3 to 4 +move 4 from 9 to 8 +move 3 from 5 to 1 +move 22 from 4 to 5 +move 1 from 7 to 2 +move 22 from 5 to 9 +move 2 from 5 to 2 +move 2 from 4 to 6 +move 10 from 9 to 5 +move 1 from 8 to 3 +move 13 from 9 to 2 +move 1 from 6 to 3 +move 19 from 2 to 7 +move 2 from 7 to 4 +move 1 from 8 to 4 +move 1 from 8 to 2 +move 11 from 5 to 7 +move 3 from 1 to 7 +move 8 from 7 to 8 +move 1 from 3 to 5 +move 1 from 8 to 3 +move 1 from 5 to 3 +move 6 from 2 to 3 +move 1 from 8 to 7 +move 1 from 6 to 1 +move 1 from 1 to 8 +move 4 from 8 to 1 +move 1 from 4 to 6 +move 8 from 3 to 9 +move 2 from 2 to 3 +move 3 from 8 to 5 +move 1 from 8 to 2 +move 4 from 2 to 7 +move 5 from 9 to 7 +move 1 from 6 to 3 +move 4 from 2 to 4 +move 23 from 7 to 5 +move 4 from 1 to 2 +move 3 from 9 to 6 +move 2 from 4 to 8 +move 2 from 8 to 3 +move 2 from 6 to 1 +move 1 from 6 to 8 +move 8 from 5 to 3 +move 5 from 2 to 6 +move 5 from 6 to 3 +move 1 from 8 to 3 +move 4 from 4 to 7 +move 15 from 5 to 2 +move 1 from 1 to 9 +move 2 from 5 to 1 +move 4 from 3 to 7 +move 1 from 4 to 9 +move 4 from 7 to 1 +move 2 from 5 to 6 +move 7 from 1 to 2 +move 6 from 2 to 3 +move 16 from 2 to 5 +move 1 from 6 to 3 +move 1 from 6 to 3 +move 9 from 7 to 4 +move 6 from 4 to 6 +move 1 from 9 to 8 +move 23 from 3 to 9 +move 1 from 3 to 4 +move 3 from 4 to 5 +move 9 from 5 to 2 +move 6 from 9 to 7 +move 7 from 7 to 5 +move 5 from 5 to 3 +move 1 from 4 to 6 +move 3 from 3 to 8 +move 6 from 2 to 1 +move 3 from 5 to 6 +move 4 from 7 to 1 +move 2 from 3 to 9 +move 5 from 6 to 8 +move 19 from 9 to 6 +move 1 from 9 to 2 +move 9 from 5 to 9 +move 4 from 8 to 3 +move 5 from 6 to 1 +move 4 from 6 to 1 +move 2 from 3 to 8 +move 17 from 1 to 7 +move 2 from 1 to 2 +move 6 from 6 to 9 +move 4 from 8 to 5 +move 3 from 8 to 2 +move 3 from 5 to 6 +move 4 from 6 to 8 +move 2 from 6 to 9 +move 4 from 8 to 7 +move 9 from 9 to 5 +move 5 from 9 to 4 +move 7 from 2 to 8 +move 1 from 2 to 1 +move 3 from 6 to 5 +move 6 from 8 to 5 +move 1 from 3 to 4 +move 1 from 3 to 1 +move 12 from 7 to 2 +move 5 from 2 to 7 +move 8 from 7 to 5 +move 1 from 9 to 3 +move 5 from 2 to 8 +move 3 from 6 to 3 +move 2 from 2 to 3 +move 1 from 2 to 4 +move 2 from 3 to 4 +move 1 from 1 to 6 +move 14 from 5 to 6 +move 1 from 8 to 6 +move 3 from 3 to 7 +move 4 from 7 to 1 +move 9 from 4 to 3 +move 3 from 1 to 4 +move 1 from 1 to 2 +move 1 from 8 to 4 +move 8 from 3 to 1 +move 1 from 3 to 2 +move 5 from 7 to 6 +move 3 from 1 to 6 +move 2 from 2 to 8 +move 13 from 5 to 3 +move 5 from 1 to 3 +move 3 from 4 to 5 +move 1 from 9 to 2 +move 4 from 3 to 9 +move 1 from 1 to 7 +move 2 from 5 to 8 +move 1 from 7 to 5 +move 2 from 5 to 4 +move 1 from 2 to 6 +move 1 from 4 to 5 +move 7 from 3 to 6 +move 31 from 6 to 1 +move 25 from 1 to 7 +move 2 from 3 to 2 +move 13 from 7 to 9 +move 1 from 1 to 6 +move 1 from 4 to 1 +move 2 from 2 to 9 +move 1 from 4 to 6 +move 3 from 7 to 1 +move 7 from 8 to 3 +move 1 from 8 to 2 +move 1 from 2 to 8 +move 4 from 3 to 4 +move 1 from 8 to 7 +move 3 from 6 to 9 +move 5 from 7 to 6 +move 1 from 4 to 7 +move 5 from 7 to 9 +move 5 from 3 to 6 +move 3 from 4 to 7 +move 1 from 5 to 4 +move 4 from 7 to 9 +move 32 from 9 to 1 +move 1 from 6 to 5 +move 1 from 5 to 9 +move 4 from 3 to 8 +move 5 from 1 to 4 +move 4 from 4 to 9 +move 6 from 1 to 7 +move 4 from 9 to 8 +move 4 from 7 to 8 +move 1 from 7 to 1 +move 1 from 7 to 6 +move 7 from 6 to 3 +move 1 from 9 to 5 +move 2 from 4 to 7 +move 25 from 1 to 6 +move 1 from 7 to 1 +move 1 from 3 to 4 +move 18 from 6 to 8 +move 1 from 5 to 1 +move 3 from 1 to 6 +move 21 from 8 to 3 +move 1 from 8 to 4 +move 2 from 4 to 2 +move 1 from 8 to 1 +move 1 from 7 to 6 +move 5 from 6 to 3 +move 30 from 3 to 1 +move 4 from 8 to 6 +move 1 from 2 to 9 +move 1 from 8 to 5 +move 9 from 6 to 5 +move 2 from 8 to 7 +move 3 from 5 to 9 +move 2 from 3 to 4 +move 1 from 2 to 1 +move 1 from 5 to 8 +move 1 from 8 to 3 +move 2 from 4 to 6 +move 1 from 3 to 1 +move 1 from 5 to 6 +move 5 from 5 to 7 +move 4 from 6 to 8 +move 3 from 8 to 2 +move 1 from 1 to 5 +move 1 from 8 to 7 +move 4 from 9 to 6 +move 1 from 5 to 1 +move 4 from 6 to 8 +move 6 from 7 to 3 +move 4 from 3 to 9 +move 2 from 2 to 7 +move 1 from 3 to 5 +move 3 from 7 to 6 +move 2 from 9 to 8 +move 1 from 2 to 4 +move 1 from 3 to 4 +move 5 from 8 to 4 +move 1 from 9 to 2 +move 1 from 7 to 5 +move 3 from 4 to 5 +move 1 from 9 to 1 +move 1 from 2 to 7 +move 1 from 7 to 1 +move 5 from 5 to 4 +move 4 from 1 to 4 +move 19 from 1 to 9 +move 6 from 6 to 2 +move 12 from 9 to 1 +move 1 from 8 to 6 +move 1 from 9 to 4 +move 4 from 4 to 8 +move 1 from 6 to 5 +move 1 from 5 to 3 +move 2 from 8 to 9 +move 5 from 4 to 6 +move 5 from 9 to 4 +move 1 from 4 to 3 +move 2 from 2 to 9 +move 1 from 6 to 5 +move 1 from 6 to 9 +move 7 from 1 to 5 +move 1 from 3 to 1 +move 2 from 8 to 3 +move 1 from 5 to 7 +move 2 from 9 to 8 diff --git a/2022/05/part1.js b/2022/05/part1.js new file mode 100644 index 0000000..c757826 --- /dev/null +++ b/2022/05/part1.js @@ -0,0 +1,67 @@ +import * as R from "remeda"; + +import { readFile } from "../03/part1.js"; + +export const sample = readFile("2022", "05", "sample"); +export const puzzleInput = readFile("2022", "05", "input"); + +function parseHeaps(text) { + const lines = text.split("\n").filter( R.identity ); + lines.reverse(); + + let [ header, ...crates ] = lines; + + const stacks = []; + + while(header.trimEnd()) { + + header = header.replace( /^(\s+)\d/, (...args) => { + crates = crates.map( c => c.slice(args[1].length) ); + + const stack = []; + + crates = crates.map( l => l.replace(/./, (c) => { + if( c !== ' ' ) stack.push(c); + return ''; + })); + + + stacks.push(stack); + + return ''; + }); + } + + return stacks; +} + +function parseCommands(text) { + return text.split("\n").filter(R.identity).map( + line => line.match(/\d+/g).map( x => parseInt(x) ) + ) +} + +function moveStacks([stacks,commands]) { + for( let [move, from, to] of commands ) { + console.log({move,from,to}); + + while(move-- > 0) { + stacks[to-1].push( stacks[from-1].pop() ); + } + + } + + return stacks; +} + +const spy = x => { console.log(x); return x; } + +export const solutionPart1 = R.createPipe( + text => text.split("\n\n"), + ([heaps,commands]) => [ parseHeaps(heaps), parseCommands(commands) ], + moveStacks, + spy, + R.map( x => x.pop() ), + x => x.join('') +) + diff --git a/2022/05/part2.js b/2022/05/part2.js new file mode 100644 index 0000000..b509eb0 --- /dev/null +++ b/2022/05/part2.js @@ -0,0 +1 @@ +import * as R from "remeda"; diff --git a/2022/05/puzzle.md b/2022/05/puzzle.md new file mode 100644 index 0000000..d56dfe0 --- /dev/null +++ b/2022/05/puzzle.md @@ -0,0 +1,78 @@ +\--- Day 5: Supply Stacks --- +---------- + +The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked *crates*, but because the needed supplies are buried under many other crates, the crates need to be rearranged. + +The ship has a *giant cargo crane* capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack. + +The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her *which* crate will end up where, and they want to be ready to unload them as soon as possible so they can embark. + +They do, however, have a drawing of the starting stacks of crates *and* the rearrangement procedure (your puzzle input). For 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 + +``` + +In this example, there are three stacks of crates. Stack 1 contains two crates: crate `Z` is on the bottom, and crate `N` is on top. Stack 2 contains three crates; from bottom to top, they are crates `M`, `C`, and `D`. Finally, stack 3 contains a single crate, `P`. + +Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration: + +``` +[D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +``` + +In the second step, three crates are moved from stack 1 to stack 3. Crates are moved *one at a time*, so the first crate to be moved (`D`) ends up below the second and third crates: + +``` + [Z] + [N] + [C] [D] + [M] [P] + 1 2 3 + +``` + +Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved *one at a time*, crate `C` ends up below crate `M`: + +``` + [Z] + [N] +[M] [D] +[C] [P] + 1 2 3 + +``` + +Finally, one crate is moved from stack 1 to stack 2: + +``` + [Z] + [N] + [D] +[C] [M] [P] + 1 2 3 + +``` + +The Elves just need to know *which crate will end up on top of each stack*; in this example, the top crates are `C` in stack 1, `M` in stack 2, and `Z` in stack 3, so you should combine these together and give the Elves the message `*CMZ*`. + +*After the rearrangement procedure completes, what crate ends up on top of each stack?* + +To begin, [get your puzzle input](5/input). + +Answer: + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Supply+Stacks%22+%2D+Day+5+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2022/05/sample b/2022/05/sample new file mode 100644 index 0000000..42ef47f --- /dev/null +++ b/2022/05/sample @@ -0,0 +1,9 @@ + [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 diff --git a/2022/05/test.js b/2022/05/test.js new file mode 100644 index 0000000..0235e1c --- /dev/null +++ b/2022/05/test.js @@ -0,0 +1,20 @@ +import { test, expect, describe } from "vitest"; + +import { expectSolution } from "../01/main.js"; +import { solutionPart1, puzzleInput,sample } from "./part1.js"; +import { solutionPart2 } from "./part2.js"; + +describe("part 1", () => { + test( "sample", () => { + expect(solutionPart1(sample)).toEqual('CMZ'); + }); + test.only("solution", () => { + expectSolution(solutionPart1(puzzleInput)).toEqual("VPCDMSLWJ"); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(solutionPart2(puzzleInput)).toEqual("TODO"); + }); +});