3rd and 2nd day

This commit is contained in:
kplaczek
2021-12-03 19:25:51 +01:00
parent 1e900f4642
commit 6365d1fef7
9 changed files with 2103 additions and 2017 deletions

19
03/part1.php Normal file
View File

@@ -0,0 +1,19 @@
<?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)));