Advertisement
Guest User

AoC_2016_DayTwentyTwo

a guest
Dec 22nd, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.04 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace AdventOfCode_Solutions
  6. {
  7.     class DayTwentyTwo_1
  8.     {
  9.         internal static void Run()
  10.         {
  11.             string[] fileLines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "DayTwentyTwo_Data.txt"));
  12.             List<List<string>> instructions = new List<List<string>>();
  13.             foreach (string line in fileLines)
  14.             {
  15.                 instructions.Add(new List<string>());
  16.                 foreach (string part in line.Split(' '))
  17.                 {
  18.                     if (!string.IsNullOrWhiteSpace(part))
  19.                     {
  20.                         instructions[instructions.Count - 1].Add(part);
  21.                     }
  22.                 }
  23.             }
  24.  
  25.             int width = 35;
  26.             int height = 25;
  27.  
  28.             foreach (List<string> instruction in instructions)
  29.             {
  30.                 if (instruction[0][0] == '/')
  31.                 {
  32.                     string[] location = instruction[0].Split('-');
  33.                     DataNode.dataNodes.Add(new DataNode(location[1], location[2], instruction[1], instruction[2], instruction[3], instruction[4]));
  34.                 }
  35.             }
  36.  
  37.             int viableCount = 0;
  38.             foreach (DataNode dataNode in DataNode.dataNodes)
  39.             {
  40.                 foreach (DataNode compareNode in DataNode.dataNodes)
  41.                 {
  42.                     if (dataNode.Viable(compareNode))
  43.                     {
  44.                         viableCount++;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             Console.WriteLine("Viable Pairs: " + viableCount);
  50.  
  51.             for (int row = 0; row < height; row++)
  52.             {
  53.                 for (int column = 0; column < width; column++)
  54.                 {
  55.                     DataNode currentNode = DataNode.FindByCoordinates(column, row);
  56.                     if (currentNode.diskSize < 100)
  57.                     {
  58.                         string nodeString = String.Format("{0, 4}", currentNode.diskPercentage + "% ");
  59.                         Console.Write(nodeString);
  60.                     }
  61.                     else
  62.                     {
  63.                         Console.Write("### ");
  64.                     }
  65.                 }
  66.                 Console.WriteLine();
  67.             }
  68.  
  69.             DataNode emptyNode = DataNode.FindEmptyNode();
  70.        
  71.             List<List<DataNode>> pathsToMainNode = new List<List<DataNode>>();
  72.             List<List<DataNode>> pathsToNextNode = new List<List<DataNode>>();
  73.             List<DataNode> pathRecord = new List<DataNode>();
  74.  
  75.             DataNode.FindByCoordinates(width - 1, 0).RecursivePath(DataNode.FindByCoordinates(0, 0), new List<DataNode>(), pathsToMainNode, null);
  76.  
  77.             List<DataNode> shortestToMain = pathsToMainNode[0];
  78.             foreach (List<DataNode> path in pathsToMainNode)
  79.             {
  80.                 if (path.Count < shortestToMain.Count)
  81.                 {
  82.                     shortestToMain = path;
  83.                 }
  84.             }
  85.  
  86.             for (int index = 1; index < shortestToMain.Count; index++)
  87.             {
  88.                 pathsToNextNode = new List<List<DataNode>>();
  89.                 emptyNode.RecursivePath(shortestToMain[index], new List<DataNode>(), pathsToNextNode, shortestToMain[index - 1]);
  90.                 List<DataNode> shortestToNextNode = pathsToNextNode[0];
  91.                 foreach (List<DataNode> path in pathsToNextNode)
  92.                 {
  93.                     if (path.Count < shortestToNextNode.Count)
  94.                     {
  95.                         shortestToNextNode = path;
  96.                     }
  97.                 }
  98.                 emptyNode = shortestToMain[index - 1];
  99.  
  100.                 foreach (DataNode node in shortestToNextNode)
  101.                 {
  102.                     pathRecord.Add(node);
  103.                 }
  104.             }
  105.  
  106.             Console.WriteLine();
  107.             Console.WriteLine("Steps To (0,0): " + pathRecord.Count);
  108.             for (int row = 0; row < height; row++)
  109.             {
  110.                 for (int column = 0; column < width; column++)
  111.                 {
  112.                     DataNode currentNode = DataNode.FindByCoordinates(column, row);
  113.                     if (currentNode.diskSize < 100)
  114.                     {
  115.                         if (pathRecord.Contains(currentNode))
  116.                         {
  117.                             Console.Write("*** ");
  118.                         }
  119.                         else
  120.                         {
  121.                             string nodeString = String.Format("{0, 4}", currentNode.diskPercentage + "% ");
  122.                             Console.Write(nodeString);
  123.                         }
  124.                     }
  125.                     else
  126.                     {
  127.                         Console.Write("### ");
  128.                     }
  129.                 }
  130.                 Console.WriteLine();
  131.             }
  132.         }
  133.  
  134.         public class DataNode
  135.         {
  136.             public static List<DataNode> dataNodes = new List<DataNode>();
  137.  
  138.             public int x;
  139.             public int y;
  140.             public int diskSize;
  141.             public int diskUsed;
  142.             public int diskAvailable;
  143.             public int diskPercentage;
  144.  
  145.             public DataNode(string x, string y, string diskSize, string diskUsed, string diskAvailable, string diskPercentage)
  146.             {
  147.                 this.x = int.Parse(x.TrimStart('x'));
  148.                 this.y = int.Parse(y.TrimStart('y'));
  149.                 this.diskSize = int.Parse(diskSize.TrimEnd('T'));
  150.                 this.diskUsed = int.Parse(diskUsed.TrimEnd('T'));
  151.                 this.diskAvailable = int.Parse(diskAvailable.TrimEnd('T'));
  152.                 this.diskPercentage = int.Parse(diskPercentage.TrimEnd('%'));
  153.             }
  154.  
  155.             public bool Viable(DataNode nodeB)
  156.             {
  157.                 if (x != nodeB.x || y != nodeB.y)
  158.                 {
  159.                     if (diskUsed > 0)
  160.                     {
  161.                         if (nodeB.diskAvailable >= diskUsed)
  162.                         {
  163.                             return true;
  164.                         }
  165.                     }
  166.                 }
  167.                 return false;
  168.             }
  169.  
  170.             public static DataNode FindByCoordinates(int x, int y)
  171.             {
  172.                 foreach (DataNode node in dataNodes)
  173.                 {
  174.                     if (node.x == x && node.y == y)
  175.                     {
  176.                         return node;
  177.                     }
  178.                 }
  179.                 return null;
  180.             }
  181.  
  182.             public void RecursivePath(DataNode finish, List<DataNode> path, List<List<DataNode>> pathsToFinish, DataNode impassibleNode)
  183.             {
  184.                 if (finish == this)
  185.                 {
  186.                     path.Add(this);
  187.                     pathsToFinish.Add(path);
  188.                 }
  189.                 else
  190.                 {
  191.                     DataNode up = FindByCoordinates(x, y - 1);
  192.                     DataNode right = FindByCoordinates(x + 1, y);
  193.                     DataNode down = FindByCoordinates(x, y + 1);
  194.                     DataNode left = FindByCoordinates(x - 1, y);
  195.  
  196.                     List<DataNode> myPath = new List<DataNode>();
  197.                     foreach (DataNode node in path)
  198.                     {
  199.                         myPath.Add(node);
  200.                     }
  201.                     myPath.Add(this);
  202.  
  203.                     if (finish.y < y)
  204.                     {
  205.                         if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
  206.                         {
  207.                             up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  208.                         }
  209.                         else
  210.                         {
  211.                             bool foundOne = false;
  212.  
  213.                             if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
  214.                             {
  215.                                 foundOne = true;
  216.                                 right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  217.                             }
  218.  
  219.                             if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
  220.                             {
  221.                                 foundOne = true;
  222.                                 left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  223.                             }
  224.  
  225.                             if (!foundOne)
  226.                             {
  227.                                 if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
  228.                                 {
  229.                                     down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  230.                                 }
  231.                             }
  232.                         }
  233.                     }
  234.                     else if (finish.x < x)
  235.                     {
  236.                         if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
  237.                         {
  238.                             left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  239.                         }
  240.                         else
  241.                         {
  242.                             bool foundOne = false;
  243.  
  244.                             if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
  245.                             {
  246.                                 foundOne = true;
  247.                                 up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  248.                             }
  249.  
  250.                             if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
  251.                             {
  252.                                 foundOne = true;
  253.                                 down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  254.                             }
  255.  
  256.                             if (!foundOne)
  257.                             {
  258.                                 if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
  259.                                 {
  260.                                     right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  261.                                 }
  262.                             }
  263.                         }
  264.                     }
  265.                     else if (finish.y > y)
  266.                     {
  267.                         if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
  268.                         {
  269.                             down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  270.                         }
  271.                         else
  272.                         {
  273.                             bool foundOne = false;
  274.  
  275.                             if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
  276.                             {
  277.                                 foundOne = true;
  278.                                 right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  279.                             }
  280.  
  281.                             if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
  282.                             {
  283.                                 foundOne = true;
  284.                                 left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  285.                             }
  286.  
  287.                             if (!foundOne)
  288.                             {
  289.                                 if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
  290.                                 {
  291.                                     up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  292.                                 }
  293.                             }
  294.                         }
  295.                     }
  296.                     else if (finish.x > x)
  297.                     {
  298.                         if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
  299.                         {
  300.                             right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  301.                         }
  302.                         else
  303.                         {
  304.                             bool foundOne = false;
  305.  
  306.                             if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
  307.                             {
  308.                                 foundOne = true;
  309.                                 up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  310.                             }
  311.  
  312.                             if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
  313.                             {
  314.                                 foundOne = true;
  315.                                 down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  316.                             }
  317.  
  318.                             if (!foundOne)
  319.                             {
  320.                                 if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
  321.                                 {
  322.                                     left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
  323.                                 }
  324.                             }
  325.                         }
  326.                     }
  327.                 }
  328.             }
  329.  
  330.             public static bool IsWall(DataNode testNode)
  331.             {
  332.                 if (testNode.diskSize > 100)
  333.                 {
  334.                     return true;
  335.                 }
  336.                 return false;
  337.             }
  338.  
  339.             public static DataNode FindEmptyNode()
  340.             {
  341.                 foreach (DataNode node in dataNodes)
  342.                 {
  343.                     if (node.diskUsed == 0)
  344.                     {
  345.                         return node;
  346.                     }
  347.                 }
  348.                 return null;
  349.             }
  350.         }
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement