This commit is contained in:
Yanick Champoux 2022-12-03 15:26:15 -05:00
parent 68b75967b6
commit 86e03e8773
3 changed files with 32 additions and 29 deletions

View File

@ -1,23 +1,20 @@
import * as R from "remeda"; import * as R from "remeda";
import fs from 'fs-extra'; import fs from "fs-extra";
import path from 'path'; import path from "path";
const readFile = (year,day,file) => fs.readFileSync( const readFile = (year, day, file) =>
path.join( year, day, file ), fs.readFileSync(path.join(year, day, file), "utf8");
"utf8"
);
export const sample = readFile('2022','03','sample'); export const sample = readFile("2022", "03", "sample");
export const puzzleInput = readFile('2022','03','input'); export const puzzleInput = readFile("2022", "03", "input");
export const solutionPart1 = R.createPipe( export const solutionPart1 = R.createPipe(
text => text.split("\n"), (text) => text.split("\n"),
R.filter(R.identity), R.filter(R.identity),
R.map( line => line.split('') ), R.map((line) => line.split("")),
R.map( line => [ line, line.splice( line.length / 2 ) ] ), R.map((line) => [line, line.splice(line.length / 2)]),
R.map( line => R.intersection(...line) ), R.map((line) => R.intersection(...line)),
R.map( line => line[0].charCodeAt(0) ), R.map((line) => line[0].charCodeAt(0)),
R.map( code => code > 96 ? code - 96 : code - 38 ), R.map((code) => (code > 96 ? code - 96 : code - 38)),
R.sumBy(R.identity) R.sumBy(R.identity)
); );

View File

@ -1,2 +1,12 @@
import * as R from "remeda"; import * as R from "remeda";
export const solutionPart2 = R.createPipe(
(text) => text.split("\n"),
R.filter(R.identity),
R.map((line) => line.split("")),
R.chunk(3),
R.map((group) => group.reduce((a, b) => R.intersection(a, b))),
R.map((line) => line[0].charCodeAt(0)),
R.map((code) => (code > 96 ? code - 96 : code - 38)),
R.sumBy(R.identity)
);

View File

@ -1,10 +1,7 @@
import { test, expect, describe } from "vitest"; import { test, expect, describe } from "vitest";
import { import { solutionPart1, sample, puzzleInput } from "./part1.js";
solutionPart1,
sample,
puzzleInput,
} from "./part1.js";
import { solutionPart2 } from "./part2.js"; import { solutionPart2 } from "./part2.js";
function expectSolution(result) { function expectSolution(result) {
@ -13,17 +10,16 @@ function expectSolution(result) {
} }
describe("part 1", () => { describe("part 1", () => {
test( 'sample', () => { test("sample", () => {
console.log(JSON.stringify(solutionPart1(sample))); expect(solutionPart1(sample)).toEqual(157);
expectSolution(solutionPart1(sample)).toEqual(157)
}); });
test("solution", () => { test("solution", () => {
expectSolution(solutionPart1(puzzleInput)).toEqual('TODO'); expectSolution(solutionPart1(puzzleInput)).toEqual(8515);
}); });
}); });
describe("part 2", () => { describe("part 2", () => {
test.todo("solution", () => { test("solution", () => {
expectSolution(solutionPart2(puzzleInput)).toEqual('TODO'); expectSolution(solutionPart2(puzzleInput)).toEqual(2434);
}); });
}); });