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