adventofcode/2023/04/part1.js

29 lines
622 B
JavaScript
Raw Normal View History

2023-12-04 17:19:52 +00:00
import * as R from "remeda";
2023-12-04 15:03:46 +00:00
export const slice_and_dice = R.createPipe(
2023-12-04 17:19:52 +00:00
(l) =>
l
.replace(/Card \d+:/, "")
.split("|")
.map((segment) => segment.split(" ").filter(R.identity)),
([winning, mine]) => {
let seen = {};
winning.forEach((n) => (seen[n] = true));
let got = mine.filter((n) => seen[n]).length;
return got;
},
(n) => (n == 0 ? 0 : Math.pow(2, n - 1))
2023-12-04 15:03:46 +00:00
);
2023-12-04 17:19:52 +00:00
export const get_lines = R.createPipe( (i) => i.split("\n"),
R.compact);
2023-12-04 15:03:46 +00:00
export function solution_1(input) {
2023-12-04 17:19:52 +00:00
return R.pipe(
input,
get_lines,
R.map(slice_and_dice),
R.sumBy(R.identity)
);
2023-12-04 15:03:46 +00:00
}