Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- namespace AdventOfCode_Solutions
- {
- class DayTwentyTwo_1
- {
- internal static void Run()
- {
- string[] fileLines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "DayTwentyTwo_Data.txt"));
- List<List<string>> instructions = new List<List<string>>();
- foreach (string line in fileLines)
- {
- instructions.Add(new List<string>());
- foreach (string part in line.Split(' '))
- {
- if (!string.IsNullOrWhiteSpace(part))
- {
- instructions[instructions.Count - 1].Add(part);
- }
- }
- }
- int width = 35;
- int height = 25;
- foreach (List<string> instruction in instructions)
- {
- if (instruction[0][0] == '/')
- {
- string[] location = instruction[0].Split('-');
- DataNode.dataNodes.Add(new DataNode(location[1], location[2], instruction[1], instruction[2], instruction[3], instruction[4]));
- }
- }
- int viableCount = 0;
- foreach (DataNode dataNode in DataNode.dataNodes)
- {
- foreach (DataNode compareNode in DataNode.dataNodes)
- {
- if (dataNode.Viable(compareNode))
- {
- viableCount++;
- }
- }
- }
- Console.WriteLine("Viable Pairs: " + viableCount);
- for (int row = 0; row < height; row++)
- {
- for (int column = 0; column < width; column++)
- {
- DataNode currentNode = DataNode.FindByCoordinates(column, row);
- if (currentNode.diskSize < 100)
- {
- string nodeString = String.Format("{0, 4}", currentNode.diskPercentage + "% ");
- Console.Write(nodeString);
- }
- else
- {
- Console.Write("### ");
- }
- }
- Console.WriteLine();
- }
- DataNode emptyNode = DataNode.FindEmptyNode();
- List<List<DataNode>> pathsToMainNode = new List<List<DataNode>>();
- List<List<DataNode>> pathsToNextNode = new List<List<DataNode>>();
- List<DataNode> pathRecord = new List<DataNode>();
- DataNode.FindByCoordinates(width - 1, 0).RecursivePath(DataNode.FindByCoordinates(0, 0), new List<DataNode>(), pathsToMainNode, null);
- List<DataNode> shortestToMain = pathsToMainNode[0];
- foreach (List<DataNode> path in pathsToMainNode)
- {
- if (path.Count < shortestToMain.Count)
- {
- shortestToMain = path;
- }
- }
- for (int index = 1; index < shortestToMain.Count; index++)
- {
- pathsToNextNode = new List<List<DataNode>>();
- emptyNode.RecursivePath(shortestToMain[index], new List<DataNode>(), pathsToNextNode, shortestToMain[index - 1]);
- List<DataNode> shortestToNextNode = pathsToNextNode[0];
- foreach (List<DataNode> path in pathsToNextNode)
- {
- if (path.Count < shortestToNextNode.Count)
- {
- shortestToNextNode = path;
- }
- }
- emptyNode = shortestToMain[index - 1];
- foreach (DataNode node in shortestToNextNode)
- {
- pathRecord.Add(node);
- }
- }
- Console.WriteLine();
- Console.WriteLine("Steps To (0,0): " + pathRecord.Count);
- for (int row = 0; row < height; row++)
- {
- for (int column = 0; column < width; column++)
- {
- DataNode currentNode = DataNode.FindByCoordinates(column, row);
- if (currentNode.diskSize < 100)
- {
- if (pathRecord.Contains(currentNode))
- {
- Console.Write("*** ");
- }
- else
- {
- string nodeString = String.Format("{0, 4}", currentNode.diskPercentage + "% ");
- Console.Write(nodeString);
- }
- }
- else
- {
- Console.Write("### ");
- }
- }
- Console.WriteLine();
- }
- }
- public class DataNode
- {
- public static List<DataNode> dataNodes = new List<DataNode>();
- public int x;
- public int y;
- public int diskSize;
- public int diskUsed;
- public int diskAvailable;
- public int diskPercentage;
- public DataNode(string x, string y, string diskSize, string diskUsed, string diskAvailable, string diskPercentage)
- {
- this.x = int.Parse(x.TrimStart('x'));
- this.y = int.Parse(y.TrimStart('y'));
- this.diskSize = int.Parse(diskSize.TrimEnd('T'));
- this.diskUsed = int.Parse(diskUsed.TrimEnd('T'));
- this.diskAvailable = int.Parse(diskAvailable.TrimEnd('T'));
- this.diskPercentage = int.Parse(diskPercentage.TrimEnd('%'));
- }
- public bool Viable(DataNode nodeB)
- {
- if (x != nodeB.x || y != nodeB.y)
- {
- if (diskUsed > 0)
- {
- if (nodeB.diskAvailable >= diskUsed)
- {
- return true;
- }
- }
- }
- return false;
- }
- public static DataNode FindByCoordinates(int x, int y)
- {
- foreach (DataNode node in dataNodes)
- {
- if (node.x == x && node.y == y)
- {
- return node;
- }
- }
- return null;
- }
- public void RecursivePath(DataNode finish, List<DataNode> path, List<List<DataNode>> pathsToFinish, DataNode impassibleNode)
- {
- if (finish == this)
- {
- path.Add(this);
- pathsToFinish.Add(path);
- }
- else
- {
- DataNode up = FindByCoordinates(x, y - 1);
- DataNode right = FindByCoordinates(x + 1, y);
- DataNode down = FindByCoordinates(x, y + 1);
- DataNode left = FindByCoordinates(x - 1, y);
- List<DataNode> myPath = new List<DataNode>();
- foreach (DataNode node in path)
- {
- myPath.Add(node);
- }
- myPath.Add(this);
- if (finish.y < y)
- {
- if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
- {
- up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- else
- {
- bool foundOne = false;
- if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
- {
- foundOne = true;
- right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
- {
- foundOne = true;
- left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (!foundOne)
- {
- if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
- {
- down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- }
- }
- }
- else if (finish.x < x)
- {
- if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
- {
- left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- else
- {
- bool foundOne = false;
- if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
- {
- foundOne = true;
- up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
- {
- foundOne = true;
- down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (!foundOne)
- {
- if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
- {
- right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- }
- }
- }
- else if (finish.y > y)
- {
- if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
- {
- down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- else
- {
- bool foundOne = false;
- if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
- {
- foundOne = true;
- right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
- {
- foundOne = true;
- left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (!foundOne)
- {
- if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
- {
- up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- }
- }
- }
- else if (finish.x > x)
- {
- if (right != null && !IsWall(right) && !myPath.Contains(right) && right != impassibleNode)
- {
- right.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- else
- {
- bool foundOne = false;
- if (up != null && !IsWall(up) && !myPath.Contains(up) && up != impassibleNode)
- {
- foundOne = true;
- up.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (down != null && !IsWall(down) && !myPath.Contains(down) && down != impassibleNode)
- {
- foundOne = true;
- down.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- if (!foundOne)
- {
- if (left != null && !IsWall(left) && !myPath.Contains(left) && left != impassibleNode)
- {
- left.RecursivePath(finish, myPath, pathsToFinish, impassibleNode);
- }
- }
- }
- }
- }
- }
- public static bool IsWall(DataNode testNode)
- {
- if (testNode.diskSize > 100)
- {
- return true;
- }
- return false;
- }
- public static DataNode FindEmptyNode()
- {
- foreach (DataNode node in dataNodes)
- {
- if (node.diskUsed == 0)
- {
- return node;
- }
- }
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement