Equd

AOC 2020 Day 10

Dec 10th, 2020
1,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. #load "xunit"
  2. #load "..\AOC Connector"
  3. #load "..\AOC 2d Array"
  4.  
  5. void Main(string[] args)
  6. {
  7.     if (args == null)
  8.         RunTests();  // Call RunTests() or press Alt+Shift+T to initiate testing.
  9.  
  10.     //get aoc data
  11.     var aoc = new AdventOfCode(2020, 10);
  12.  
  13.     //solve A  
  14.     var answerA = SolveA(aoc.InputLines);
  15.     aoc.SubmitAnswer(answerA.d1 * answerA.d3, Part.A);
  16.    
  17.     //solve B
  18.     aoc.SubmitAnswer(SolveB(aoc.InputLines), Part.B);
  19. }
  20.  
  21. public (int d1, int d3) SolveA(string[] input)
  22. {
  23.     //add first 0, and last + 3
  24.     var adaptors = CreateListWithSocketAndLast(input);
  25.    
  26.     //store deltas
  27.     var lstDeltas = new List<int>();   
  28.     for(int i = 0; i < adaptors.Length - 1; i++)
  29.     {
  30.         lstDeltas.Add(adaptors[i + 1] - adaptors[i]);
  31.     }
  32.    
  33.     //done
  34.     return (lstDeltas.Count(x => x == 1), lstDeltas.Count(x => x == 3));
  35. }
  36.  
  37. public long SolveB(string[] input)
  38. {
  39.     //final answer is stored here, it is multiplication so start with 1
  40.     long result = 1;
  41.    
  42.     //repeating combo results are stored here
  43.     int[] resultCaching = new int[10];
  44.    
  45.     //get data
  46.     var adaptors = CreateListWithSocketAndLast(input);
  47.    
  48.     //start of current slice
  49.     int startOfCurrentSlice = 0;
  50.    
  51.     //loop through the data
  52.     for (int i = 0; i < adaptors.Length  - 1; i++)
  53.     {
  54.         //next step is 3, so get all posibble
  55.         if(adaptors[i + 1] - adaptors[i] == 3)
  56.         {
  57.             //determin length
  58.             int setLength = i - startOfCurrentSlice + 1;
  59.            
  60.             //not cached?
  61.             if (resultCaching[setLength] == 0)
  62.             {
  63.                 var set = adaptors[startOfCurrentSlice..(i+1)];
  64.                 resultCaching[setLength] = GetAllPossibe(set, new List<int>(), 0).Distinct().Count();
  65.             }
  66.             result *= resultCaching[setLength];
  67.             startOfCurrentSlice = i + 1;
  68.         }
  69.     }
  70.     //done
  71.     return result;
  72.  
  73.     //quick recursive search for all possible
  74.     IEnumerable<string> GetAllPossibe(int[] input, List<int> lst, int startIndex)
  75.     {
  76.         //valid combo?
  77.         if (lst.Count == 0 || input[startIndex] - lst.Last() <= 3)
  78.         {
  79.             //create new list, and add item
  80.             lst = new List<int>(lst);
  81.             lst.Add(input[startIndex]);
  82.  
  83.             //are we finished?
  84.             if (input.Length - 1 == startIndex)
  85.                 yield return string.Join("", lst);
  86.             else
  87.             {
  88.                 //try all other combos, start and the next item in the array
  89.                 for (int i = startIndex + 1; i < input.Length; i++)
  90.                 {
  91.                     foreach (var element in GetAllPossibe(input, lst, i))
  92.                     {
  93.                         yield return element;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. int[] CreateListWithSocketAndLast(string[] lines)
  102. {
  103.     var result = new List<int> { 0 };
  104.     result.AddRange(lines.Select(x => int.Parse(x)));
  105.     result.Add(result.Max() + 3);
  106.     result = result.OrderBy(x => x).ToList();
  107.     return result.ToArray();
  108. }
  109.  
  110.  
  111. #region private::Tests
  112. #region test data
  113. string[] testData = new []
  114. {
  115. "16",
  116. "10",
  117. "15",
  118. "5",
  119. "1",
  120. "11",
  121. "7",
  122. "19",
  123. "6",
  124. "12",
  125. "4",
  126. };
  127. string[] testData2 = new []
  128. {
  129. "28",
  130. "33",
  131. "18",
  132. "42",
  133. "31",
  134. "14",
  135. "46",
  136. "20",
  137. "48",
  138. "47",
  139. "24",
  140. "23",
  141. "49",
  142. "45",
  143. "19",
  144. "38",
  145. "39",
  146. "11",
  147. "1",
  148. "32",
  149. "25",
  150. "35",
  151. "8",
  152. "17",
  153. "7",
  154. "9",
  155. "4",
  156. "2",
  157. "34",
  158. "10",
  159. "3",
  160. };
  161.  
  162. #endregion
  163. //tests
  164. [Fact] void TestA1() => Assert.Equal((7,5), SolveA(testData));
  165. [Fact] void TestA2() => Assert.Equal((22, 10), SolveA(testData2));
  166. [Fact] void TestB1() => Assert.Equal(8, SolveB(testData));
  167. [Fact] void TestB2() => Assert.Equal(19208, SolveB(testData2));
  168. #endregion
  169.  
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment