adventofcode/2022/04/part1.js

21 lines
591 B
JavaScript
Raw Normal View History

2022-12-04 17:37:18 +00:00
import * as R from "remeda";
2022-12-04 17:42:17 +00:00
import { readFile } from "../03/part1.js";
2022-12-04 17:37:18 +00:00
export const sample = readFile("2022", "04", "sample");
export const puzzleInput = readFile("2022", "04", "input");
2022-12-04 17:42:17 +00:00
const rangeLength = ([a, b]) => b - a + 1;
2022-12-04 17:37:18 +00:00
2022-12-04 17:42:17 +00:00
const isContainedBy = ([a1, a2], [b1, b2]) => a1 >= b1 && a2 <= b2;
2022-12-04 17:37:18 +00:00
export const solutionPart1 = R.createPipe(
(text) => text.split("\n"),
R.filter(R.identity),
2022-12-04 17:42:17 +00:00
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))
2022-12-04 17:37:18 +00:00
);