19 lines
667 B
PHP
19 lines
667 B
PHP
<?php
|
|
|
|
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
$input = array_map(fn($line) => str_split($line), $input);
|
|
|
|
$o2 = $co2 = $input;
|
|
|
|
for ($i = 0; $i < count($input[0]); $i++) {
|
|
if (count($o2) > 1) {
|
|
$bit = (int)(array_sum($column = array_column($o2, $i)) >= ceil(count($column) / 2));
|
|
$o2 = array_filter($o2, fn($line) => $line[$i] == $bit);
|
|
}
|
|
|
|
if (count($co2) > 1) {
|
|
$bit = (int) (array_sum($column = array_column($co2, $i)) >= ceil(count($column) / 2));
|
|
$co2 = array_filter($co2, fn ($line) => $line[$i] != $bit);
|
|
}
|
|
}
|
|
echo bindec(implode('', reset($co2))) * bindec(implode('', reset($o2))); |