Answer ⏱ 2ms
Part 1 :
1867
Part 2 :
84538
$games = $input->lines->map(function ($l) {
$matches = $l->matchAll("/([0-9]+) ([a-z]+)/");
$game = [];
foreach ($matches[2] as $i => $color) {
$game[$color] = max(($game[$color]??0), $matches[1][$i]);
}
return $game;
});
// ==================================================
// > PART 1
// ==================================================
$rule = ["red" => 12, "green" => 13, "blue" => 14];
$possible = $games->filter(function ($game) use ($rule) {
foreach ($rule as $color => $value) {
if ($game[$color] > $value) return false;
}
return true;
});
$solution_1 = $possible->keys()->sum() + $possible->count();
// ==================================================
// > PART 2
// ==================================================
$solution_2 = $games->callEach()->multiply()->sum();