Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace AdventOfCode_Solutions
- {
- class DayThirteen_1
- {
- private const int WRITING_PAUSE_TIME = 10;
- public static List<List<Coordinate>> pathsToEnd;
- internal static void Run()
- {
- int mazeWidth = -1, mazeHeight = -1, specialNumber = -1, goalX = -1, goalY = -1;
- GetInput(ref mazeWidth, ref mazeHeight, ref specialNumber, ref goalX, ref goalY);
- Coordinate[,] mazeMap = new Coordinate[mazeWidth, mazeHeight];
- pathsToEnd = new List<List<Coordinate>>();
- FillMaze(mazeMap, mazeWidth, mazeHeight, specialNumber, goalX, goalY);
- DrawMaze(mazeMap, mazeWidth, mazeHeight, goalX, goalY);
- mazeMap[1, 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, new List<Coordinate>());
- List<Coordinate> shortestPath = pathsToEnd[0];
- foreach (List<Coordinate> path in pathsToEnd)
- {
- if (path.Count < shortestPath.Count)
- {
- shortestPath = path;
- }
- }
- foreach (Coordinate coord in shortestPath)
- {
- coord.state = Coordinate.TileState.FinalPath;
- }
- DrawMaze(mazeMap, mazeWidth, mazeHeight, goalX, goalY);
- Console.WriteLine("Shortest Path Steps: " + (shortestPath.Count));
- //Console.WriteLine("Paths To End: " + pathsToEnd.Count);
- //for (int count = 0; count < pathsToEnd.Count; count++)
- //{
- // Console.WriteLine("Path " + count + ": " + (pathsToEnd[count].Count + 1) + " Steps");
- //}
- }
- private static void GetInput(ref int mazeWidth, ref int mazeHeight, ref int specialNumber, ref int goalX, ref int goalY)
- {
- Console.Write("Maze Width: ");
- while (!int.TryParse(Console.ReadLine(), out mazeWidth))
- {
- Console.Write("Sorry, that was not an integer. Please try again: ");
- }
- Console.Write("Maze Height: ");
- while (!int.TryParse(Console.ReadLine(), out mazeHeight))
- {
- Console.Write("Sorry, that was not an integer. Please try again: ");
- }
- Console.Write("Favourite Number: ");
- while (!int.TryParse(Console.ReadLine(), out specialNumber))
- {
- Console.Write("Sorry, that was not an integer. Please try again: ");
- }
- Console.Write("Goal X: ");
- while (!int.TryParse(Console.ReadLine(), out goalX))
- {
- Console.Write("Sorry, that was not an integer. Please try again: ");
- }
- Console.Write("Goal Y: ");
- while (!int.TryParse(Console.ReadLine(), out goalY))
- {
- Console.Write("Sorry, that was not an integer. Please try again: ");
- }
- }
- private static void FillMaze(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int specialNumber, int goalX, int goalY)
- {
- for (int column = 0; column < mazeWidth; column++)
- {
- for (int row = 0; row < mazeHeight; row++)
- {
- int value = (column * column) + (3 * column) + (2 * column * row) + row + (row * row) + specialNumber;
- string binaryString = Convert.ToString(value, 2);
- int oneCount = 0;
- foreach (char number in binaryString)
- {
- if (number == '1')
- {
- oneCount++;
- }
- }
- if (oneCount % 2 == 0)
- {
- mazeMap[column, row] = new Coordinate(column, row, Coordinate.TileState.Floor);
- }
- else
- {
- mazeMap[column, row] = new Coordinate(column, row, Coordinate.TileState.Wall);
- }
- }
- }
- mazeMap[goalX, goalY].state = Coordinate.TileState.End;
- }
- private static void DrawMaze(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int goalX, int goalY)
- {
- Console.WriteLine("CUBICLES OF THE ENDLESS...");
- for (int index = 0; index < mazeWidth + 1; index++)
- {
- //System.Threading.Thread.Sleep(WRITING_PAUSE_TIME);
- if (index > 0)
- {
- //Console.Write(index - 1);
- Console.Write('-');
- }
- else
- {
- Console.Write(" ");
- }
- }
- Console.WriteLine();
- for (int row = 0; row < mazeHeight; row++)
- {
- //System.Threading.Thread.Sleep(WRITING_PAUSE_TIME);
- //Console.Write(row);
- Console.Write('|');
- for (int column = 0; column < mazeHeight; column++)
- {
- Console.Write(mazeMap[column, row].symbol);
- }
- Console.WriteLine();
- }
- }
- public class Coordinate
- {
- public int x;
- public int y;
- public char symbol
- {
- get
- {
- if (x == 1 && y == 1)
- {
- return 'S';
- }
- switch (state)
- {
- case TileState.Floor:
- return '.';
- case TileState.Wall:
- return '#';
- case TileState.Visited:
- return 'V';
- case TileState.FinalPath:
- return 'F';
- case TileState.End:
- return 'E';
- default:
- return '?';
- }
- }
- }
- public enum TileState
- {
- Floor,
- Wall,
- Visited,
- FinalPath,
- End
- }
- public TileState state;
- public Coordinate(int x, int y, TileState state)
- {
- this.x = x;
- this.y = y;
- this.state = state;
- }
- public static bool FindByCoord(List<Coordinate> coordsToCheck, Coordinate comparison)
- {
- foreach (Coordinate coord in coordsToCheck)
- {
- if(comparison.x == coord.x && comparison.y == coord.y)
- {
- return true;
- }
- }
- return false;
- }
- public void RecursivePath(Coordinate[,] mazeMap, int mazeWidth, int mazeHeight, int goalX, int goalY, List<Coordinate> senderPath)
- {
- if (state == TileState.End)
- {
- pathsToEnd.Add(senderPath);
- }
- else if (state != TileState.Wall && !FindByCoord(senderPath, this))
- {
- state = TileState.Visited;
- List<Coordinate> myPath = new List<Coordinate>();
- foreach (Coordinate coord in senderPath)
- {
- myPath.Add(coord);
- }
- myPath.Add(this);
- if (x + 1 < mazeWidth)
- {
- mazeMap[x + 1, y].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
- }
- if (x - 1 >= 0)
- {
- mazeMap[x - 1, y].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
- }
- if (y + 1 < mazeHeight)
- {
- mazeMap[x, y + 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
- }
- if (y - 1 >= 0)
- {
- mazeMap[x, y - 1].RecursivePath(mazeMap, mazeWidth, mazeHeight, goalX, goalY, myPath);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement