advent_of_code_2021/day_2/part_1.php

23 lines
538 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;
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);