Finit le jour 5

This commit is contained in:
Clément 2020-12-08 16:38:00 +01:00
parent 22e1882391
commit 1f389e52aa
2 changed files with 101 additions and 0 deletions

48
day_5/part_1.php Normal file
View File

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

53
day_5/part_2.php Normal file
View File

@ -0,0 +1,53 @@
<?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;
}
}