Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Security.Cryptography;
- using System.Collections.Generic;
- namespace AdventOfCode_Solutions
- {
- class DaySeventeen_1
- {
- public static List<string> pathsToEnd;
- public const string CODE = "dmypynyp";
- internal static void Run()
- {
- DateTime start = DateTime.Now;
- pathsToEnd = new List<string>();
- Room[,] rooms = new Room[4, 4];
- for (int column = 0; column < 4; column++)
- {
- for (int row = 0; row < 4; row++)
- {
- rooms[column, row] = new Room();
- }
- }
- for (int column = 0; column < 4; column++)
- {
- for (int row = 0; row < 4; row++)
- {
- if (row != 0)
- {
- rooms[column, row].upConnection = rooms[column, row - 1];
- }
- if (row != 3)
- {
- rooms[column, row].downConnection = rooms[column, row + 1];
- }
- if (column != 0)
- {
- rooms[column, row].leftConnection = rooms[column - 1, row];
- }
- if (column != 3)
- {
- rooms[column, row].rightConnection = rooms[column + 1, row];
- }
- }
- }
- rooms[3, 3].isExit = true;
- rooms[0, 0].RecursivePath("");
- string shortestPath = pathsToEnd[0];
- foreach (string path in pathsToEnd)
- {
- if (path.Length < shortestPath.Length)
- {
- shortestPath = path;
- }
- }
- string longestPath = pathsToEnd[0];
- foreach (string path in pathsToEnd)
- {
- if (path.Length > longestPath.Length)
- {
- longestPath = path;
- }
- }
- Console.WriteLine("Shortest Path: " + shortestPath);
- Console.WriteLine("Longest Path Length: " + longestPath.Length);
- Console.WriteLine("Time: " + (DateTime.Now - start));
- }
- private static string CreateHash(string input)
- {
- MD5 md5 = MD5.Create();
- byte[] md5Hash = md5.ComputeHash(Encoding.ASCII.GetBytes(input));
- StringBuilder sb = new StringBuilder();
- for (int count = 0; count < 2; count++)
- {
- sb.Append(md5Hash[count].ToString("X2"));
- }
- return sb.ToString().ToLower();
- }
- public class Room
- {
- public Room upConnection;
- public Room downConnection;
- public Room rightConnection;
- public Room leftConnection;
- public bool isExit;
- public Room()
- {
- upConnection = null;
- downConnection = null;
- rightConnection = null;
- leftConnection = null;
- isExit = false;
- }
- public void RecursivePath(string senderPath)
- {
- if (isExit)
- {
- pathsToEnd.Add(senderPath);
- }
- else
- {
- string hash = CreateHash(CODE + senderPath);
- if (upConnection != null)
- {
- if (!Locked(0, hash))
- {
- upConnection.RecursivePath(senderPath + 'U');
- }
- }
- if (rightConnection != null)
- {
- if (!Locked(3, hash))
- {
- rightConnection.RecursivePath(senderPath + 'R');
- }
- }
- if (downConnection != null)
- {
- if (!Locked(1, hash))
- {
- downConnection.RecursivePath(senderPath + 'D');
- }
- }
- if (leftConnection != null)
- {
- if (!Locked(2, hash))
- {
- leftConnection.RecursivePath(senderPath + 'L');
- }
- }
- }
- }
- public static bool Locked(int index, string hashCode)
- {
- int holder;
- if (int.TryParse(hashCode[index].ToString(), out holder) || hashCode[index] == 'a')
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement