adventofcode/2022/13/part1.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-13 17:58:12 +00:00
import * as R from "remeda";
import { readFile } from "../05/part1.js";
const readInput = (...args) =>
R.pipe(
readFile(...args),
(lines) => lines.split("\n\n"),
R.compact, // remove last line
2022-12-13 17:58:27 +00:00
R.map((block) => block.split("\n").map((line) => eval(line)))
2022-12-13 17:58:12 +00:00
);
export const puzzleInput = readInput(import.meta.url, "input");
export const sample = readInput(import.meta.url, "sample");
2022-12-13 17:58:27 +00:00
export function isLessThan(x, y) {
if ([x, y].every(R.isNumber)) return x < y ? -1 : x == y ? 0 : 1;
2022-12-13 17:58:12 +00:00
2022-12-13 17:58:27 +00:00
if ([x, y].every(R.isArray)) {
for (const [a, b] of R.zip(x, y)) {
const res = isLessThan(a, b);
if (res !== 0) return res;
2022-12-13 17:58:12 +00:00
}
2022-12-13 17:58:27 +00:00
if (x.length === y.length) return 0;
return x.length < y.length ? -1 : 1;
}
2022-12-13 17:58:12 +00:00
2022-12-13 17:58:27 +00:00
return isLessThan(...[x, y].map((z) => (R.isNumber(z) ? [z] : z)));
2022-12-13 17:58:12 +00:00
}
const passthru = (x) => (arg) => {
2022-12-13 17:58:27 +00:00
x(arg);
return arg;
};
2022-12-13 17:58:12 +00:00
export default R.createPipe(
2022-12-13 17:58:27 +00:00
R.map(([x, y]) => isLessThan(x, y)),
(results) => results.map((value, index) => ({ value, index: index + 1 })),
R.filter(({ value }) => value !== 1),
R.map(R.prop("index")),
(results) => results.reduce((a, b) => a + b)
2022-12-13 17:58:12 +00:00
);