[2015-05-01] Challenge #212 [Hard] Reverse Maze Pathfinding

This is my C# solution using a file IO to read the inputs: <pre><code> using System; using System.Collections.Generic; using System.IO;

namespace Hard212ReverseMaze { class Program { //delegate for an instruction delegate void moveSetDelegate(ref Position p, ref int dx, ref int dy);

    static void Main(string[] args)
    {
        //read input from file
        HashSet<Position> allowableSpaces = new HashSet<Position>();
        string input;
        using (StreamReader sr = new StreamReader(@"C:\Users\Ferdy\Documents\Visual Studio 2013\Projects\Hard-212-ReverseMaze\maze-long.txt"))
        {
            int height = int.Parse(sr.ReadLine());
            for (int i = 0; i < height; i++)
            {
                string line = sr.ReadLine();
                //get set of allowable spaces in the maze
                for (int j = 0; j < line.Length; j++)
                {
                    if (line[j] == ' ')
                    {
                        allowableSpaces.Add(new Position(j, i));
                    }
                }
            }
            input = sr.ReadLine();
        }

        //traverse through set of instructions
        int dirX, dirY;
        Position pos;
        List<moveSetDelegate> moveSet = new List<moveSetDelegate>();
        //for each instruction, add a method to the delegate corresponding to the instruction
        for (int i = 0; i < input.Length; i++)
        {
            string curDirection = input[i].ToString();
            int moveAmount;
            if (int.TryParse(curDirection, out moveAmount))
            {
                for (int j = 0; j < moveAmount; j++)
                {
                    moveSet.Add(moveForward);
                }
            }
            else
            {
                if (curDirection == "l")
                {
                    moveSet.Add(turnLeft);
                }
                else if (curDirection == "r")
                {
                    moveSet.Add(turnRight);
                }
            }
        }
        //traverse through possible solutions set
        foreach (Position p in allowableSpaces)
        {
            foreach (int[] dirs in new List<int[]>() {new int[]{0,1}, new int[]{1,0}, new int[]{0,-1}, new int[]{-1,0}})
            {
                pos = p;
                dirX = dirs[0];
                dirY = dirs[1];
                bool validPath = true;
                //for each possible starting solution, invoke the delegate incrementally (hence the list instead of simply adding)
                foreach (moveSetDelegate m in moveSet)
                {
                    m(ref pos, ref dirX, ref dirY);
                    //if we went out of bounds, then break out of the loop because it's not a valid path
                    if (!allowableSpaces.Contains(pos))
                    {
                        validPath = false;
                        break;
                    }
                }
                //if we didn't hit any walls, then we're good to go!
                if (validPath)
                {
                    Console.WriteLine(String.Format("Start Position: ({0},{1}); Start Direction: ({2},{3})", p.x, p.y, dirs[0], dirs[1]));
                    Console.WriteLine(String.Format("End Position: ({0},{1}); End Direction: ({2},{3})", pos.x, pos.y, dirX, dirY));
                }
            }
        }
        Console.WriteLine("End of Routine");
        Console.ReadKey();
    }

    static void turnLeft(ref Position p, ref int dx, ref int dy)
    {
        int temp = dy;
        dy = -dx;
        dx = temp;
    }

    static void turnRight(ref Position p, ref int dx, ref int dy)
    {
        int temp = dx;
        dx = -dy;
        dy = temp;
    }

    static void moveForward(ref Position p, ref int dx, ref int dy)
    {
        p.y += dy;
        p.x += dx;
    }
}

//convenient struct for a pair of x,y coordinates
struct Position
{
    public int x;
    public int y;

    public Position(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

} </pre></code>

/r/dailyprogrammer Thread