From 86e03e87736bc4e7ecbdbebf5cc11238fe3e7526 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 3 Dec 2022 15:26:15 -0500 Subject: [PATCH] part 2 --- 2022/03/part1.js | 31 ++++++++++++++----------------- 2022/03/part2.js | 10 ++++++++++ 2022/03/test.js | 20 ++++++++------------ 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/2022/03/part1.js b/2022/03/part1.js index a7d31f2..023ee91 100644 --- a/2022/03/part1.js +++ b/2022/03/part1.js @@ -1,23 +1,20 @@ import * as R from "remeda"; -import fs from 'fs-extra'; -import path from 'path'; +import fs from "fs-extra"; +import path from "path"; -const readFile = (year,day,file) => fs.readFileSync( - path.join( year, day, file ), - "utf8" -); +const readFile = (year, day, file) => + fs.readFileSync(path.join(year, day, file), "utf8"); -export const sample = readFile('2022','03','sample'); -export const puzzleInput = readFile('2022','03','input'); +export const sample = readFile("2022", "03", "sample"); +export const puzzleInput = readFile("2022", "03", "input"); export const solutionPart1 = R.createPipe( - text => text.split("\n"), - R.filter( R.identity ), - R.map( line => line.split('') ), - R.map( line => [ line, line.splice( line.length / 2 ) ] ), - R.map( line => R.intersection(...line) ), - R.map( line => line[0].charCodeAt(0) ), - R.map( code => code > 96 ? code - 96 : code - 38 ), - R.sumBy( R.identity ) + (text) => text.split("\n"), + R.filter(R.identity), + R.map((line) => line.split("")), + R.map((line) => [line, line.splice(line.length / 2)]), + R.map((line) => R.intersection(...line)), + R.map((line) => line[0].charCodeAt(0)), + R.map((code) => (code > 96 ? code - 96 : code - 38)), + R.sumBy(R.identity) ); - diff --git a/2022/03/part2.js b/2022/03/part2.js index 31477e8..b40e97e 100644 --- a/2022/03/part2.js +++ b/2022/03/part2.js @@ -1,2 +1,12 @@ 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) +); diff --git a/2022/03/test.js b/2022/03/test.js index 95bdb5f..4f1e0f8 100644 --- a/2022/03/test.js +++ b/2022/03/test.js @@ -1,10 +1,7 @@ import { test, expect, describe } from "vitest"; -import { - solutionPart1, - sample, - puzzleInput, -} from "./part1.js"; +import { solutionPart1, sample, puzzleInput } from "./part1.js"; + import { solutionPart2 } from "./part2.js"; function expectSolution(result) { @@ -13,17 +10,16 @@ function expectSolution(result) { } describe("part 1", () => { - test( 'sample', () => { - console.log(JSON.stringify(solutionPart1(sample))); - expectSolution(solutionPart1(sample)).toEqual(157) - }); + test("sample", () => { + expect(solutionPart1(sample)).toEqual(157); + }); test("solution", () => { - expectSolution(solutionPart1(puzzleInput)).toEqual('TODO'); + expectSolution(solutionPart1(puzzleInput)).toEqual(8515); }); }); describe("part 2", () => { - test.todo("solution", () => { - expectSolution(solutionPart2(puzzleInput)).toEqual('TODO'); + test("solution", () => { + expectSolution(solutionPart2(puzzleInput)).toEqual(2434); }); });