This commit is contained in:
Yanick Champoux 2022-12-04 12:37:18 -05:00
parent 33a11a02dd
commit 26f9e6f512
4 changed files with 52 additions and 0 deletions

20
2022/04/part1.js Normal file
View File

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

2
2022/04/part2.js Normal file
View File

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

6
2022/04/sample Normal file
View File

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

24
2022/04/test.js Normal file
View File

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