adventofcode/2023/04/part2.js
2023-12-04 12:19:52 -05:00

32 lines
779 B
JavaScript

import * as R from "remeda";
import { get_lines } from "./part1.js";
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;
}