From d9bf1d7d21cdcdf7acaa48b9a10021e9487b1b25 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 4 Dec 2022 12:42:17 -0500 Subject: [PATCH] part 2 --- 2022/03/part1.js | 2 +- 2022/04/part1.js | 16 ++++++++-------- 2022/04/part2.js | 11 +++++++++++ 2022/04/test.js | 17 ++++++++--------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/2022/03/part1.js b/2022/03/part1.js index 023ee91..7366b96 100644 --- a/2022/03/part1.js +++ b/2022/03/part1.js @@ -2,7 +2,7 @@ import * as R from "remeda"; import fs from "fs-extra"; import path from "path"; -const readFile = (year, day, file) => +export const readFile = (year, day, file) => fs.readFileSync(path.join(year, day, file), "utf8"); export const sample = readFile("2022", "03", "sample"); diff --git a/2022/04/part1.js b/2022/04/part1.js index 5c5acd2..7a346cb 100644 --- a/2022/04/part1.js +++ b/2022/04/part1.js @@ -1,20 +1,20 @@ import * as R from "remeda"; -import { readFile } from '../03/part1.js'; +import { readFile } from "../03/part1.js"; export const sample = readFile("2022", "04", "sample"); export const puzzleInput = readFile("2022", "04", "input"); +const rangeLength = ([a, b]) => b - a + 1; -const rangeLength = ([a,b]) => b-a+1; - -const isContainedBy = ( [a1,a2],[b1,b2] ) => - ( (a1 >= b1) && (a2 <= b2) ); +const isContainedBy = ([a1, a2], [b1, b2]) => a1 >= b1 && a2 <= b2; export const solutionPart1 = R.createPipe( (text) => text.split("\n"), R.filter(R.identity), - R.map( line => line.split(',').map( range => range.split('-').map( x => parseInt(x) ) ) ), - R.map( R.sortBy( x => rangeLength(x) ) ), - R.countBy( ([a,b]) => isContainedBy(a,b) ) + R.map((line) => + line.split(",").map((range) => range.split("-").map((x) => parseInt(x))) + ), + R.map(R.sortBy((x) => rangeLength(x))), + R.countBy(([a, b]) => isContainedBy(a, b)) ); diff --git a/2022/04/part2.js b/2022/04/part2.js index 31477e8..744c09b 100644 --- a/2022/04/part2.js +++ b/2022/04/part2.js @@ -1,2 +1,13 @@ import * as R from "remeda"; +const overlapsWith = ([a1, a2], [b1, b2]) => a2 >= b1; + +export const solutionPart2 = R.createPipe( + (text) => text.split("\n"), + R.filter(R.identity), + R.map((line) => + line.split(",").map((range) => range.split("-").map((x) => parseInt(x))) + ), + R.map(R.sortBy(([x]) => x)), + R.countBy(([a, b]) => overlapsWith(a, b)) +); diff --git a/2022/04/test.js b/2022/04/test.js index 9cf5b07..a96bd6e 100644 --- a/2022/04/test.js +++ b/2022/04/test.js @@ -1,11 +1,7 @@ -import { test, expect, describe } from "vitest"; +import { test, describe } from "vitest"; import { expectSolution } from "../01/main.js"; -import { - solutionPart1, - puzzleInput, - sample, -} from "./part1.js"; +import { solutionPart1, puzzleInput, sample } from "./part1.js"; import { solutionPart2 } from "./part2.js"; describe("part 1", () => { @@ -13,12 +9,15 @@ describe("part 1", () => { expectSolution(solutionPart1(sample)).toEqual(2); }); test("solution", () => { - expectSolution(solutionPart1(puzzleInput)).toEqual('TODO'); + expectSolution(solutionPart1(puzzleInput)).toEqual(605); }); }); describe("part 2", () => { - test.todo("solution", () => { - expectSolution(solutionPart2(puzzleInput)).toEqual('TODO'); + test("sample", () => { + expectSolution(solutionPart2(sample)).toEqual(4); + }); + test("solution", () => { + expectSolution(solutionPart2(puzzleInput)).toEqual(914); }); });