Merge branch '2022-05'

This commit is contained in:
Yanick Champoux 2022-12-07 18:36:16 -05:00
commit 471be2c493
4 changed files with 99 additions and 0 deletions

69
2022/05/part1.js Normal file
View File

@ -0,0 +1,69 @@
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("")
);

1
2022/05/part2.js Normal file
View File

@ -0,0 +1 @@
import * as R from "remeda";

9
2022/05/sample Normal file
View File

@ -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

20
2022/05/test.js Normal file
View File

@ -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");
});
});