✨ Finit le jour 2
This commit is contained in:
parent
a6efae14f9
commit
0dc0c90964
23
day_2/part_1.php
Normal file
23
day_2/part_1.php
Normal 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
25
day_2/part_2.php
Normal 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);
|
Loading…
Reference in New Issue
Block a user