advent_of_code_2020/day_5/part_2.php

53 lines
1.1 KiB
PHP
Raw Normal View History

2020-12-08 16:38:00 +01:00
<?php
$ids = explode("\n", file_get_contents('input.txt'));
//$ids = ['FBFBBFFRLR'];
$seat_ids = [];
function decodeRow($row)
{
$min = 1;
$max = 128;
foreach (str_split($row) as $cell) {
if ($cell === 'F') {
$max = floor($min + ($max - $min) / 2);
} else {
$min = ceil($max - ($max - $min) / 2);
}
}
return $min - 1;
}
function decodeColumn($column)
{
$min = 1;
$max = 8;
foreach (str_split($column) as $cell) {
if ($cell === 'L') {
$max = floor($min + ($max - $min) / 2);
} else {
$min = ceil($max - ($max - $min) / 2);
}
}
return $min - 1;
}
foreach ($ids as $id) {
$encoded_row = substr($id, 0, 7);
$encoded_column = substr($id, 7);
$row = decodeRow($encoded_row);
$column = decodeColumn($encoded_column);
$seat_ids[] = $row * 8 + $column;
}
sort($seat_ids);
for ($i = current($seat_ids), $l = end($seat_ids), reset($seat_ids); $i <= $l; $i++) {
if (!in_array($i, $seat_ids, true)) {
echo $i;
}
}