let pad = [ '123', '456', '789' ]; let code = ''; let [i, j] = [1, 1]; while let $line = read() { for c in line { let [ni, nj] = match c { 'U' => [i - 1, j], 'D' => [i + 1, j], 'L' => [i, j - 1], 'R' => [i, j + 1] }; let valid = k -> (..3).contains?(k); if (valid(ni) && valid(nj)) [i, j] = [ni, nj]; } code += pad[i][j]; } print(code);