adventofcode/2023/04/part2.js

32 lines
779 B
JavaScript
Raw Permalink Normal View History

2023-12-04 17:19:52 +00:00
import * as R from "remeda";
2023-12-04 15:03:46 +00:00
2023-12-04 17:19:52 +00:00
import { get_lines } from "./part1.js";
2023-12-04 15:03:46 +00:00
2023-12-04 17:19:52 +00:00
export function solution_2(input) {
const cards = get_lines(input);
const cards_won = cards.map(()=>1);
let total_won = 0;
while( cards.length ) {
// console.log(cards_won);
let card = cards.shift();
const [ winning, mine ] = card
.replace(/Card \d+:/, "")
.split("|")
.map((segment) => segment.split(" ").filter(R.identity));
let seen = {};
winning.forEach((n) => (seen[n] = true));
let got = mine.filter((n) => seen[n]).length;
R.range(1,got+1).forEach( i => {
cards_won[i] = (cards_won[i] ?? 0 ) +cards_won[0];
});
total_won += cards_won.shift()??0;
}
return total_won;
}