Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace D11._1
  6. {
  7. public enum EnumElement
  8. {
  9. Strontium = 0,
  10. Plutonium = 1,
  11. Thulium = 2,
  12. Ruthenium = 3,
  13. Curium = 4,
  14. Elerium = 5,
  15. Dilithium = 6
  16. }
  17. public enum EnumType
  18. {
  19. Generator = 0,
  20. Microship = 1
  21. }
  22. public class Facility : ICloneable
  23. {
  24. public Floor[] Floors { get; private set; }
  25. public int Elevator { get; set; }
  26. // All floors are empty except for the last one
  27. public bool IsOk()
  28. {
  29. for (var i = 0; i < Floors.Length - 1; ++i)
  30. if (Floors[i].IsEmpty() == false) return false;
  31. return true;
  32. }
  33. public Facility()
  34. {
  35. Floors = new Floor[4];
  36. for (var i = 0; i < 4; ++i)
  37. Floors[i] = new Floor();
  38. }
  39. public Floor CurrentFloor => Floors[Elevator];
  40. public class Floor : ICloneable
  41. {
  42. public bool[][] Objects = new bool[2][];
  43. public bool IsEmpty()
  44. {
  45. for (var i = 0; i < Objects[0].Length; ++i)
  46. if (Objects[(int)EnumType.Generator][i] || Objects[(int)EnumType.Microship][i]) return false;
  47. return true;
  48. }
  49. public bool IsValid()
  50. {
  51. int generators = 0;
  52. int unpluggedMicroships = 0;
  53. for (var i = 0; i < Objects[0].Length; ++i)
  54. {
  55. if (Objects[(int)EnumType.Generator][i]) generators++;
  56. if (Objects[(int)EnumType.Generator][i] && Objects[(int)EnumType.Microship][i]) continue;
  57. if (Objects[(int)EnumType.Microship][i]) unpluggedMicroships++;
  58. }
  59. return generators >= 0 && unpluggedMicroships == 0;
  60. }
  61. static readonly int NElements = Enum.GetValues(typeof(EnumElement)).Length;
  62. public Floor()
  63. {
  64. for (int i = 0; i < Objects.Length; ++i)
  65. Objects[i] = new bool[NElements];
  66. }
  67. public object Clone()
  68. {
  69. var floor = new Floor();
  70. for (var i = 0; i < Objects.Length; i++)
  71. Objects[i].CopyTo(floor.Objects[i], 0);
  72. return floor;
  73. }
  74. public bool Set(EnumType type, EnumElement element, bool value) => Objects[(int)type][(int)element] = value;
  75. public bool Add(EnumType type, EnumElement element) => Set(type, element, true);
  76. public override int GetHashCode()
  77. {
  78. //int h1 = 0, h2 = 0;
  79. //for (var i = 0; i < Objects[0].Length; ++i)
  80. //{
  81. // h1 += (Objects[0][i] ? 1 : 0) << i;
  82. // h2 += (Objects[1][i] ? 1 : 0) << i;
  83. //}
  84. //return h1 * 100 + h2;
  85. /*
  86. * Firts hash method takes into account wich microship and generator element is
  87. * present on the floor.
  88. *
  89. * The following methods just relies on the fact that it does not matter
  90. * which generators or microships are on the floor but that there are
  91. * paired microship/generators (it does not matter wich ones) and unpaired
  92. * microships and generators (still, it does not matter which ones)
  93. *
  94. * * ==> ENORMOUS performance gain : ~8s => 150ms
  95. */
  96. int paired = 0, unpairedGen = 0, unpairedMicroship = 0;
  97. for (var i = 0; i < Objects[0].Length; ++i)
  98. {
  99. if (Objects[(int)EnumType.Microship][i] && Objects[(int)EnumType.Generator][i])
  100. {
  101. paired++;
  102. continue;
  103. }
  104. if (Objects[(int)EnumType.Microship][i]) unpairedMicroship++;
  105. if (Objects[(int)EnumType.Generator][i]) unpairedGen++;
  106. }
  107. return (((paired << 3) + unpairedGen) << 3) + unpairedMicroship;
  108. }
  109. }
  110. public override int GetHashCode()
  111. {
  112. int hash = 0;
  113. foreach (var floor in Floors) hash = HashCode.Combine(hash, floor.GetHashCode());
  114. return hash;
  115. }
  116. public object Clone()
  117. {
  118. var nf = new Facility
  119. {
  120. Elevator = this.Elevator,
  121. Floors = new Floor[Floors.Length]
  122. };
  123. for (var i = 0; i < Floors.Length; ++i) nf.Floors[i] = (Floor)Floors[i].Clone();
  124. return nf;
  125. }
  126. }
  127. class Program
  128. {
  129. static readonly EnumElement[] AllElements = Enum.GetValues(typeof(EnumElement)) as EnumElement[];
  130. static readonly int[] ElevatorDirections = new[] { -1, +1 };
  131. static int RunFacilityTest(Facility startFacility)
  132. {
  133. var queue = new Queue<(Facility facility, int step)>();
  134. var tested = new HashSet<int>();
  135. queue.Enqueue((startFacility, 0));
  136. while (queue.Count > 0)
  137. {
  138. (Facility facility, int step) = queue.Dequeue();
  139. if (facility.IsOk()) return step;
  140. foreach (((EnumType, EnumElement) first, (EnumType, EnumElement)? second) in GetCandidates(facility))
  141. {
  142. foreach (var direction in ElevatorDirections)
  143. {
  144. if (facility.Elevator + direction < 0 || facility.Elevator + direction >= facility.Floors.Length) continue;
  145. Facility nfacility = BuildNewFacility(facility, first, second, direction);
  146. if (nfacility == null) continue;
  147. int hash = nfacility.GetHashCode() ^ nfacility.Elevator;
  148. if (tested.Contains(hash)) continue;
  149. tested.Add(hash);
  150. queue.Enqueue((nfacility, step + 1));
  151. }
  152. }
  153. }
  154. throw new Exception();
  155. }
  156. private static Facility BuildNewFacility(Facility facility, (EnumType t, EnumElement e) first, (EnumType t, EnumElement e)? second, int direction)
  157. {
  158. /* Slight optimisation
  159. * Before cloning the entire facility, ensures that the next state for the current floor
  160. * is valid as cloning a single floor is less consuming.
  161. */
  162. var clone = facility.Floors[facility.Elevator + direction].Clone() as Facility.Floor;
  163. clone.Set(first.t, first.e, true);
  164. if (second.HasValue) clone.Set(second.Value.t, second.Value.e, true);
  165. if (clone.IsValid() == false) return null;
  166. // -----------
  167. var nfacility = facility.Clone() as Facility;
  168. nfacility.CurrentFloor.Set(first.t, first.e, false);
  169. if (second.HasValue) nfacility.CurrentFloor.Set(second.Value.t, second.Value.e, false);
  170. nfacility.Elevator += direction;
  171. nfacility.Floors[nfacility.Elevator] = clone;
  172. return nfacility;
  173. }
  174. private static HashSet<((EnumType, EnumElement), (EnumType, EnumElement)?)> GetCandidates(Facility facility)
  175. {
  176. var all = new List<(EnumType, EnumElement)>();
  177. foreach (EnumElement element in AllElements)
  178. {
  179. if (facility.CurrentFloor.Objects[(int)EnumType.Generator][(int)element]) all.Add((EnumType.Generator, element));
  180. if (facility.CurrentFloor.Objects[(int)EnumType.Microship][(int)element]) all.Add((EnumType.Microship, element));
  181. }
  182. var comb = new HashSet<((EnumType, EnumElement), (EnumType, EnumElement)?)>();
  183. foreach ((EnumType t1, EnumElement e1) in all)
  184. {
  185. foreach ((EnumType t2, EnumElement e2) in all)
  186. {
  187. if (t1 == t2 && e1 == e2)
  188. {
  189. comb.Add(((t1, e1), null));
  190. continue;
  191. }
  192. if (comb.Contains(((t2, e2), (t1, e1)))) continue;
  193. comb.Add(((t1, e1), (t2, e2)));
  194. }
  195. }
  196. return comb;
  197. }
  198. static void Main(string[] args)
  199. {
  200. if (args.Length < 1) throw new ArgumentException();
  201. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  202. Console.WriteLine( "=== PART 1 ========");
  203. Part1();
  204. Console.WriteLine("\n=== PART 2 ========");
  205. Part2();
  206. }
  207. private static void Part1()
  208. {
  209. var facility = new Facility();
  210. BaseFacility(facility);
  211. var sw = Stopwatch.StartNew();
  212. var answer = RunFacilityTest(facility);
  213. Console.WriteLine($"The answer is : {answer}");
  214. sw.Stop();
  215. Console.WriteLine($"\nExecution time : {sw.ElapsedMilliseconds}ms");
  216. }
  217. private static void BaseFacility(Facility facility)
  218. {
  219. facility.Floors[0].Add(EnumType.Generator, EnumElement.Strontium);
  220. facility.Floors[0].Add(EnumType.Microship, EnumElement.Strontium);
  221. facility.Floors[0].Add(EnumType.Generator, EnumElement.Plutonium);
  222. facility.Floors[0].Add(EnumType.Microship, EnumElement.Plutonium);
  223. facility.Floors[1].Add(EnumType.Generator, EnumElement.Thulium);
  224. facility.Floors[1].Add(EnumType.Generator, EnumElement.Ruthenium);
  225. facility.Floors[1].Add(EnumType.Microship, EnumElement.Ruthenium);
  226. facility.Floors[1].Add(EnumType.Generator, EnumElement.Curium);
  227. facility.Floors[1].Add(EnumType.Microship, EnumElement.Curium);
  228. facility.Floors[2].Add(EnumType.Microship, EnumElement.Thulium);
  229. }
  230. private static void Part2()
  231. {
  232. var facility = new Facility();
  233. BaseFacility(facility);
  234. facility.Floors[0].Add(EnumType.Generator, EnumElement.Elerium);
  235. facility.Floors[0].Add(EnumType.Microship, EnumElement.Elerium);
  236. facility.Floors[0].Add(EnumType.Generator, EnumElement.Dilithium);
  237. facility.Floors[0].Add(EnumType.Microship, EnumElement.Dilithium);
  238. var sw = Stopwatch.StartNew();
  239. var answer = RunFacilityTest(facility);
  240. Console.WriteLine($"The answer is : {answer}");
  241. sw.Stop();
  242. Console.WriteLine($"\nExecution time : {sw.ElapsedMilliseconds}ms");
  243. }
  244. }
  245. }