advent_of_code_2021/day_2/part_2.php

25 lines
581 B
PHP
Raw Permalink Normal View History

2021-12-02 16:23:18 +01:00
<?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);