Advertisement
Guest User

AoC_2016_DayThirteen

a guest
Dec 13th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace AdventOfCode_Solutions
  5. {
  6.     class DayThirteen_1
  7.     {
  8.         private const int WRITING_PAUSE_TIME = 10;
  9.         public static List<List<Coordinate>> pathsToEnd;
  10.  
  11.         internal static void Run()
  12.         {
  13.             int mazeWidth = -1, mazeHeight = -1, specialNumber = -1, goalX = -1, goalY = -1;
  14.             GetInput(ref mazeWidth, ref mazeHeight, ref specialNumber, ref goalX, ref goalY);
  15.  
  16.             Coordinate[,] mazeMap = new Coordinate[mazeWidth, mazeHeight];
  17.             pathsToEnd = new List<List<Coordinate>>();
  18.  
  19.             FillMaze(mazeMap, mazeWidth, mazeHeight, specialNumber, goalX, goalY);
  20.             DrawMaze(mazeMap, mazeWidth, mazeHeight, goalX, goalY);
  21.             mazeMap[1, 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, new List<Coordinate>());
  22.  
  23.             List<Coordinate> shortestPath = pathsToEnd[0];
  24.             foreach (List<Coordinate> path in pathsToEnd)
  25.             {
  26.                 if (path.Count < shortestPath.Count)
  27.                 {
  28.                     shortestPath = path;
  29.                 }
  30.             }
  31.             foreach (Coordinate coord in shortestPath)
  32.             {
  33.                 coord.state = Coordinate.TileState.FinalPath;
  34.             }
  35.             DrawMaze(mazeMap, mazeWidth, mazeHeight, goalX, goalY);
  36.             Console.WriteLine("Shortest Path Steps: " + (shortestPath.Count));
  37.             //Console.WriteLine("Paths To End: " + pathsToEnd.Count);
  38.             //for (int count = 0; count < pathsToEnd.Count; count++)
  39.             //{
  40.             //    Console.WriteLine("Path " + count + ": " + (pathsToEnd[count].Count + 1) + " Steps");
  41.             //}
  42.         }
  43.  
  44.         private static void GetInput(ref int mazeWidth, ref int mazeHeight, ref int specialNumber, ref int goalX, ref int goalY)
  45.         {
  46.             Console.Write("Maze Width: ");
  47.             while (!int.TryParse(Console.ReadLine(), out mazeWidth))
  48.             {
  49.                 Console.Write("Sorry, that was not an integer. Please try again: ");
  50.             }
  51.  
  52.             Console.Write("Maze Height: ");
  53.             while (!int.TryParse(Console.ReadLine(), out mazeHeight))
  54.             {
  55.                 Console.Write("Sorry, that was not an integer. Please try again: ");
  56.             }
  57.  
  58.             Console.Write("Favourite Number: ");
  59.             while (!int.TryParse(Console.ReadLine(), out specialNumber))
  60.             {
  61.                 Console.Write("Sorry, that was not an integer. Please try again: ");
  62.             }
  63.  
  64.             Console.Write("Goal X: ");
  65.             while (!int.TryParse(Console.ReadLine(), out goalX))
  66.             {
  67.                 Console.Write("Sorry, that was not an integer. Please try again: ");
  68.             }
  69.  
  70.             Console.Write("Goal Y: ");
  71.             while (!int.TryParse(Console.ReadLine(), out goalY))
  72.             {
  73.                 Console.Write("Sorry, that was not an integer. Please try again: ");
  74.             }
  75.         }
  76.  
  77.         private static void FillMaze(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int specialNumber, int goalX, int goalY)
  78.         {
  79.             for (int column = 0; column < mazeWidth; column++)
  80.             {
  81.                 for (int row = 0; row < mazeHeight; row++)
  82.                 {
  83.                     int value = (column * column) + (3 * column) + (2 * column * row) + row + (row * row) + specialNumber;
  84.                     string binaryString = Convert.ToString(value, 2);
  85.                     int oneCount = 0;
  86.                     foreach (char number in binaryString)
  87.                     {
  88.                         if (number == '1')
  89.                         {
  90.                             oneCount++;
  91.                         }
  92.                     }
  93.                     if (oneCount % 2 == 0)
  94.                     {
  95.                         mazeMap[column, row] = new Coordinate(column, row, Coordinate.TileState.Floor);
  96.                     }
  97.                     else
  98.                     {
  99.                         mazeMap[column, row] = new Coordinate(column, row, Coordinate.TileState.Wall);
  100.                     }
  101.                 }
  102.             }
  103.             mazeMap[goalX, goalY].state = Coordinate.TileState.End;
  104.         }
  105.  
  106.         private static void DrawMaze(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int goalX, int goalY)
  107.         {
  108.             Console.WriteLine("CUBICLES OF THE ENDLESS...");
  109.             for (int index = 0; index < mazeWidth + 1; index++)
  110.             {
  111.                 //System.Threading.Thread.Sleep(WRITING_PAUSE_TIME);
  112.                 if (index > 0)
  113.                 {
  114.                     //Console.Write(index - 1);
  115.                     Console.Write('-');
  116.                 }
  117.                 else
  118.                 {
  119.                     Console.Write(" ");
  120.                 }
  121.             }
  122.             Console.WriteLine();
  123.             for (int row = 0; row < mazeHeight; row++)
  124.             {
  125.                 //System.Threading.Thread.Sleep(WRITING_PAUSE_TIME);
  126.                 //Console.Write(row);
  127.                 Console.Write('|');
  128.                 for (int column = 0; column < mazeHeight; column++)
  129.                 {
  130.                     Console.Write(mazeMap[column, row].symbol);
  131.                 }
  132.                 Console.WriteLine();
  133.             }
  134.         }
  135.  
  136.         public class Coordinate
  137.         {
  138.             public int x;
  139.             public int y;
  140.             public char symbol
  141.             {
  142.                 get
  143.                 {
  144.                     if (x == 1 && y == 1)
  145.                     {
  146.                         return 'S';
  147.                     }
  148.                     switch (state)
  149.                     {
  150.                         case TileState.Floor:
  151.                             return '.';
  152.  
  153.                         case TileState.Wall:
  154.                             return '#';
  155.  
  156.                         case TileState.Visited:
  157.                             return 'V';
  158.  
  159.                         case TileState.FinalPath:
  160.                             return 'F';
  161.  
  162.                         case TileState.End:
  163.                             return 'E';
  164.                         default:
  165.                             return '?';
  166.                     }
  167.                 }
  168.             }
  169.             public enum TileState
  170.             {
  171.                 Floor,
  172.                 Wall,
  173.                 Visited,
  174.                 FinalPath,
  175.                 End
  176.             }
  177.             public TileState state;
  178.  
  179.             public Coordinate(int x, int y, TileState state)
  180.             {
  181.                 this.x = x;
  182.                 this.y = y;
  183.                 this.state = state;
  184.             }
  185.  
  186.             public static bool FindByCoord(List<Coordinate> coordsToCheck, Coordinate comparison)
  187.             {
  188.                 foreach (Coordinate coord in coordsToCheck)
  189.                 {
  190.                     if(comparison.x == coord.x && comparison.y == coord.y)
  191.                     {
  192.                         return true;
  193.                     }
  194.                 }
  195.                 return false;
  196.             }
  197.  
  198.             public void RecursivePath(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int goalX, int goalY, List<Coordinate> senderPath)
  199.             {
  200.                 if (state == TileState.End)
  201.                 {
  202.                     pathsToEnd.Add(senderPath);
  203.                 }
  204.                 else if (state != TileState.Wall && !FindByCoord(senderPath, this))
  205.                 {
  206.                     state = TileState.Visited;
  207.                     List<Coordinate> myPath = new List<Coordinate>();
  208.                     foreach (Coordinate coord in senderPath)
  209.                     {
  210.                         myPath.Add(coord);
  211.                     }
  212.                     myPath.Add(this);
  213.                     if (x + 1 < mazeWidth)
  214.                     {
  215.                         mazeMap[x + 1, y].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
  216.                     }
  217.                     if (x - 1 >= 0)
  218.                     {
  219.                         mazeMap[x - 1, y].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
  220.                     }
  221.                     if (y + 1 < mazeHeight)
  222.                     {
  223.                         mazeMap[x, y + 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
  224.                     }
  225.                     if (y - 1 >= 0)
  226.                     {
  227.                         mazeMap[x, y - 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement