advent_of_code_2020/day_5/part_1.php

48 lines
953 B
PHP

<?php
$ids = explode("\n", file_get_contents('input.txt'));
//$ids = ['FBFBBFFRLR'];
$max_seat_id = 0;
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);
$max_seat_id = max($max_seat_id, ($row * 8 + $column));
}
echo $max_seat_id;