Files
advent_of_code_2021/03/part2.php
kplaczek fd05fbc8e5 4th day
2021-12-04 10:31:35 +01:00

19 lines
659 B
PHP

<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = array_map('str_split', $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))); //2990784