// ==================================================
// > PART 1
// ==================================================
$gamma = grid($input, 1)->columns()->map(function ($col) {
return $col->mostCommon();
})->join();
$epsilon = $gamma->replace("0", "x")->replace("1", "0")->replace("x", "1");
$solution_1 = base_convert($gamma->int, 2, 10) * base_convert($epsilon->int, 2, 10);
// ==================================================
// > PART 2
// ==================================================
$oxygen = grid($input, 1);
for ($x = 0; $x < $oxygen->width(); $x++) {
$bit = $oxygen->column($x)->sort()->mostCommon();
$oxygen = grid($oxygen->rows->filter(fn ($row) => $row[$x]->int == $bit)->values());
if ($oxygen->height() == 1) break;
}
$co2 = grid($input, 1);
for ($x = 0; $x < $co2->width(); $x++) {
$bit = $co2->column($x)->sort()->reverse()->leastCommon();
$co2 = grid($co2->rows->filter(fn ($row) => $row[$x]->int == $bit)->values());
if ($co2->height() == 1) break;
}
$solution_2 = base_convert($oxygen[0]->join()->int, 2, 10) * base_convert($co2[0]->join()->int, 2, 10);