adventofcode/2022/04/part1.js
2022-12-04 12:43:11 -05:00

21 lines
591 B
JavaScript

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