private static string PressKeypad(char [,] pad, string[] moves) { var pos = pad.PositionsOf('5').First(); var pincode = moves. Select(line => { foreach (var ch in line) { var newpos = ch switch { 'U' => pos.Up, 'D' => pos.Down, 'R' => pos.Right, 'L' => pos.Left, _ => throw new Exception($"Unknown move {ch}") }; if (pad.CharAt(newpos) != ' ') { pos = newpos; } ; } return(pad.CharAt(pos)); }) .ToArray(); return(new string(pincode)); }