✨ Finit le jour 2 et commence le 1 et le 3
This commit is contained in:
35
day_1/part_1.php
Normal file
35
day_1/part_1.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
$lines = array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line));
|
||||
|
||||
$count = 0;
|
||||
$position = 50;
|
||||
|
||||
echo 'The dial starts by pointing at 50'."\n";
|
||||
|
||||
function checkPosition(&$position)
|
||||
{
|
||||
if ($position < 0) {
|
||||
$position = 100 - abs($position);
|
||||
} elseif ($position > 99) {
|
||||
$position = $position - 100;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$direction = substr($line, 0, 1);
|
||||
$number = (int)substr($line, 1) % 100;
|
||||
if ($direction === 'L') {
|
||||
$position -= $number;
|
||||
checkPosition($position);
|
||||
} elseif ($direction === 'R') {
|
||||
$position += $number;
|
||||
checkPosition($position);
|
||||
}
|
||||
|
||||
if ($position === 0) {
|
||||
$count++;
|
||||
}
|
||||
|
||||
echo sprintf('The dial is rotated %s to point at %u | count : %u%s', $line,$position, $count, "\n");
|
||||
}
|
||||
Reference in New Issue
Block a user