Part 2 :
function solve($pad, $moves)
{
$pos = xy($pad->search("5"));
$code = "";
foreach ($moves as $line) {
foreach ($line->chars() as $d) {
$pos->direction(direction($d));
if (!$pad->has($pos)) $pos->back();
}
$code .= $pad->get($pos);
}
return $code;
}
$solution_1 = solve(grid("123\n456\n789"), $input->lines);
$solution_2 = solve(grid(" 1\n 234\n56789\n ABC\n D")->filter(), $input->lines);