This commit is contained in:
Yanick Champoux 2022-12-04 12:42:17 -05:00
parent 26f9e6f512
commit d9bf1d7d21
4 changed files with 28 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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