advent_of_code_2021/day_2/part_1.php
2021-12-02 16:23:18 +01:00

23 lines
538 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);