From c1a05285126b0c3415f86a63c3d161a76a9ab8ed 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/part1.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 2022/05/part2.js | 1 + 2022/05/sample | 9 +++++++ 2022/05/test.js | 20 +++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 2022/05/part1.js create mode 100644 2022/05/part2.js create mode 100644 2022/05/sample create mode 100644 2022/05/test.js 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/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"); + }); +});