Answer ⏱ 6ms
Part 1 :
23847
Part 2 :
8570000
$wins = $input->lines->map(function ($l) {
[$_, $l] = $l->split(":");
[$winners, $have] = $l->split("|");
return $have->numbers()->keep($winners->numbers())->count();
});
// ==================================================
// > PART 1
// ==================================================
$solution_1 = $wins->map(fn ($w) => $w ? 2 ** ($w - 1) : 0)->sum();
// ==================================================
// > PART 2
// ==================================================
$cards = $wins->map(fn () => 1);
foreach ($cards as $i => $card) {
for ($j = 1; $j <= $wins[$i]; $j++) {
$cards[$i + $j] += $card;
}
}
$solution_2 = $cards->sum();