Guest User

Untitled

a guest
Dec 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace Infi
  7. {
  8. public static class Program
  9. {
  10. /// <summary>
  11. /// Defines the entry point of the application.
  12. /// </summary>
  13. public static void Main()
  14. {
  15. Console.WriteLine($"De uitkomst van deel 1 is: {Infi(GetInput())}");
  16. Console.ReadLine();
  17. }
  18.  
  19. /// <summary>
  20. /// Infi AoC challenge, bereken de clashes, visualiseer daarna de clashes.
  21. /// </summary>
  22. /// <param name="input">The input.</param>
  23. /// <returns></returns>
  24. private static int Infi(string input)
  25. {
  26. var locationlist = new List<int[]>();
  27. var robotList = new List<int[]>();
  28.  
  29. var clashLocations = new List<int[]>();
  30.  
  31. (robotList, locationlist) = ModifyInput(input);
  32.  
  33. var robotSwitcher = 0;
  34. var maxWidth = 0;
  35.  
  36. foreach(var location in locationlist)
  37. {
  38. robotList[robotSwitcher][0] += location[0];
  39. robotList[robotSwitcher][1] += location[1];
  40.  
  41. if (robotList[0][0] == robotList[1][0] && robotList[0][1] == robotList[1][1])
  42. {
  43. clashLocations.Add(new int[] { robotList[0][0], robotList[0][1] });
  44. maxWidth = Math.Max(maxWidth, robotList[0][0]);
  45. }
  46.  
  47. robotSwitcher = (robotSwitcher + 1) % robotList.Count;
  48. }
  49.  
  50. // Deel 2
  51. DrawMap(clashLocations, maxWidth);
  52.  
  53. return clashLocations.Count;
  54. }
  55.  
  56. /// <summary>
  57. /// Draws the map.
  58. /// </summary>
  59. /// <param name="locations">The locations.</param>
  60. /// <param name="maxWidth">The maximum width.</param>
  61. private static void DrawMap(List<int[]> locations, int maxWidth)
  62. {
  63. var map = new Dictionary<int, string[]>();
  64.  
  65. foreach (var location in locations)
  66. {
  67. if (!map.ContainsKey(location[1])) {
  68. map.Add(location[1], new string[maxWidth + 1]);
  69.  
  70. for (int i = 0; i < map[location[1]].Length; i++)
  71. {
  72. map[location[1]][i] = " ";
  73. }
  74. }
  75.  
  76. map[location[1]][location[0]] = "#";
  77. }
  78.  
  79. foreach (var key in map.Keys.OrderBy(x => x))
  80. {
  81. foreach (var arrValue in map[key])
  82. {
  83. Console.Write(arrValue);
  84. }
  85.  
  86. Console.WriteLine();
  87. }
  88. }
  89.  
  90. /// <summary>
  91. /// Modifies the input.
  92. /// </summary>
  93. /// <param name="input">The input.</param>
  94. /// <returns>two lists, one with the robots, the second the locations</returns>
  95. private static (List<int[]> robotList, List<int[]> locationlist) ModifyInput(string input) {
  96. var locationlist = new List<int[]>();
  97. var robotList = new List<int[]>();
  98. bool robot = false;
  99. bool location = false;
  100. var start = 0;
  101. var length = 0;
  102.  
  103. for (int i = 0; i < input.Length; i++)
  104. {
  105. if (input[i].Equals('['))
  106. {
  107. start = i + 1;
  108. robot = true;
  109. }
  110.  
  111. if (input[i].Equals('('))
  112. {
  113. start = i + 1;
  114. location = true;
  115. }
  116.  
  117.  
  118. if ((input[i].Equals(']') && robot) || (input[i].Equals(')') && location))
  119. {
  120. length = i - start;
  121. var locationString = input.Substring(start, length);
  122. var locationArr = locationString.Split(',').Select(x => int.Parse(x)).ToArray();
  123.  
  124. if(robot)
  125. {
  126. robotList.Add(locationArr);
  127. robot = false;
  128. }
  129. else if (location)
  130. {
  131. locationlist.Add(locationArr);
  132. location = false;
  133. }
  134. }
  135. }
  136.  
  137. return (robotList, locationlist);
  138. }
  139.  
  140. /// <summary>
  141. /// Gets the input.
  142. /// </summary>
  143. /// <returns></returns>
  144. private static string GetInput() => File.ReadLines(@"..\..\..\Input\Infi.txt").First();
  145. }
  146. }
Add Comment
Please, Sign In to add comment