diff --git a/2022/04/part1.js b/2022/04/part1.js new file mode 100644 index 0000000..5c5acd2 --- /dev/null +++ b/2022/04/part1.js @@ -0,0 +1,20 @@ +import * as R from "remeda"; + +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 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) ) +); diff --git a/2022/04/part2.js b/2022/04/part2.js new file mode 100644 index 0000000..31477e8 --- /dev/null +++ b/2022/04/part2.js @@ -0,0 +1,2 @@ +import * as R from "remeda"; + diff --git a/2022/04/sample b/2022/04/sample new file mode 100644 index 0000000..9f9e9cf --- /dev/null +++ b/2022/04/sample @@ -0,0 +1,6 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8 diff --git a/2022/04/test.js b/2022/04/test.js new file mode 100644 index 0000000..9cf5b07 --- /dev/null +++ b/2022/04/test.js @@ -0,0 +1,24 @@ +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", () => { + expectSolution(solutionPart1(sample)).toEqual(2); + }); + test("solution", () => { + expectSolution(solutionPart1(puzzleInput)).toEqual('TODO'); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(solutionPart2(puzzleInput)).toEqual('TODO'); + }); +});