advent_of_code_2021/day_3/part_1.php

16 lines
416 B
PHP

<?php
$lines = array_map('str_split', explode("\n", file_get_contents('input.txt')));
$lines = array_map('array_reverse', array_map(null, ...$lines)); // matrix rotation
$gamma = '';
$epsilon = '';
foreach ($lines as $line) {
$counts = array_count_values($line);
$max = array_search(max($counts), $counts);
$gamma .= $max;
$epsilon .= $max === 1 ? 0 : 1;
}
echo bindec($gamma) * bindec($epsilon);