Finit le jour 2

This commit is contained in:
Clement Desmidt 2021-12-02 16:23:18 +01:00
parent a6efae14f9
commit 0dc0c90964
2 changed files with 48 additions and 0 deletions

23
day_2/part_1.php Normal file
View File

@ -0,0 +1,23 @@
<?php
$instructions = explode("\n", file_get_contents('input.txt'));
$position = 0;
$depth = 0;
foreach ($instructions as $instruction) {
[$movement, $value] = explode(' ', $instruction);
switch ($movement) {
case 'forward':
$position += $value;
break;
case 'up':
$depth -= $value;
break;
case 'down':
$depth += $value;
break;
}
}
echo sprintf('horizontal position %s × depth %s = %s', $position, $depth, $position * $depth);

25
day_2/part_2.php Normal file
View File

@ -0,0 +1,25 @@
<?php
$instructions = explode("\n", file_get_contents('input.txt'));
$position = 0;
$depth = 0;
$aim = 0;
foreach ($instructions as $instruction) {
[$movement, $value] = explode(' ', $instruction);
switch ($movement) {
case 'forward':
$position += $value;
$depth += $aim * $value;
break;
case 'up':
$aim -= $value;
break;
case 'down':
$aim += $value;
break;
}
}
echo sprintf('horizontal position %s × depth %s = %s', $position, $depth, $position * $depth);