Advertisement
eltea

Advent of Code 2020 Day 10 Parts 1 and 2

Dec 11th, 2020 (edited)
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //Advent of Code 2020 Day 10 Parts 1 and 2 solution by Mike LeSauvage
  6. public class AdapterAnalysis : MonoBehaviour
  7. {
  8.     [SerializeField] TextAsset joltageTextfile = null;   //Hooked up in the input text in the Unity editor.
  9.    
  10.     List<int> joltages = new List<int>();
  11.  
  12.  
  13.     void Start()
  14.     {
  15.         string[] joltageStrings = joltageTextfile.text.Split('\n');
  16.  
  17.         PrepareJoltages(joltageStrings); //Adds the starting joltage, adapter joltage, and sorts them.
  18.  
  19.         SolvePartOne();
  20.         SolvePartTwo();
  21.     }
  22.  
  23.     void SolvePartTwo()
  24.     {
  25.         //Get list of length of all continguous 1-jolt adapters gaps.
  26.         //The adapters on the ends are not counted as they are required to be in the sequence to support their 3-jolt gap.
  27.         //Eg: 0    3 4 5 6 7 8    11 -> count is 5 (4-8)
  28.         //    0    3 4 5     8       -> count is 1 (4)
  29.         //    0    3 4 5 6      9    -> count is 2 (4-6)
  30.  
  31.         List<int> oneJoltRunLengths = new List<int>();
  32.         int contiguousCount = 0;
  33.         for(int i=0; i<joltages.Count-1; i++)
  34.         {
  35.             if(joltages[i+1] - joltages[i] == 1)
  36.             {
  37.                 contiguousCount++;
  38.             }
  39.             else
  40.             {
  41.                 contiguousCount--;
  42.                 if (contiguousCount >= 1)
  43.                 {
  44.                     oneJoltRunLengths.Add(contiguousCount);
  45.                 }
  46.                 contiguousCount = 0;
  47.             }
  48.  
  49.         }
  50.  
  51.         //Getting the total number of combinations means finding how many ways in which adapters can be left unused from sequence
  52.         //Adapters can be left out as long as they don't create a three-jolt gap.
  53.         //The list of oneJoltRunLengths has been prepared as counts of
  54.         //  runs of adapters that can be removed in that they're not required to span a 3-jolt gap (see above). So the question is, how many
  55.         //  combinations are there for a run of 1-jolt adapter gaps that don't create a gap of 3 or more.
  56.         //For a single adapter, there are two ways.  (0, 1)
  57.         //For two adapters in a row, there are four. (00, 01, 10, 11)
  58.         //For three, there are seven ways            (001, 010, 011, 100, 101, 110, 111 -> just not 000)
  59.         //Past that point, this approach can be used:
  60.         //    https://math.stackexchange.com/questions/2844818/coin-tossing-problem-where-three-tails-come-in-a-row
  61.         //
  62.         //However, the data set as provided only had gaps of 1, 2, and 3 jolts, so the solutions for those gaps are fixed in the
  63.         //  array runCombinations rather than a generalized solution.
  64.         //To get the total combination, multiply the number of combinations of each run by each other!
  65.         long totalCombinations = 1;
  66.         int[] runCombinations = { 1, 2, 4, 7 };
  67.         foreach(int c in oneJoltRunLengths)
  68.         {
  69.             totalCombinations *= runCombinations[c];
  70.         }
  71.  
  72.         Debug.Log($"Total number of adapter combinations: {totalCombinations}");
  73.     }
  74.  
  75.  
  76.  
  77.     void SolvePartOne()
  78.     {
  79.         int[] joltageRangeCounts = new int[3];  //Stores the histogram of joltages for gaps of 1,2, and 3 (in positions 0,1,2)
  80.         GenerateJoltageHistogram(joltageRangeCounts);
  81.         int oneJoltDifferences = joltageRangeCounts[0];
  82.         int threeJoltDifferences = joltageRangeCounts[2];
  83.  
  84.  
  85.         Debug.Log($"1-jolt Differences: {oneJoltDifferences}");
  86.         Debug.Log($"3-jolt Differences: {threeJoltDifferences}");
  87.         Debug.Log($"1-jolt differences multiplied by 3-jolt differences: { oneJoltDifferences * threeJoltDifferences}");
  88.     }
  89.  
  90.     //Creates bins of joltage gaps (1, 2, and 3).
  91.     //Assumes there are adapters to span all gaps (no 4-jolt and larger gaps!)
  92.     void GenerateJoltageHistogram(int[] joltageRangeCounts)
  93.     {
  94.         for (int i = 0; i < joltages.Count - 1; i++)
  95.         {
  96.             int joltageRange = joltages[i + 1] - joltages[i];
  97.  
  98.             joltageRangeCounts[joltageRange - 1]++;
  99.         }
  100.     }
  101.  
  102.     //Add in the zero-joltage start point and the device's built-in 3-jolt adapater.
  103.     //Sorts the list from smallest to largest.
  104.     void PrepareJoltages(string[] joltageStrings)
  105.     {
  106.         //Seed with the outlet joltage.
  107.         joltages.Add(0);
  108.  
  109.         foreach (string s in joltageStrings)
  110.         {
  111.             joltages.Add(int.Parse(s));
  112.         }
  113.  
  114.         joltages.Sort();
  115.  
  116.         //Add the device's joltage to the end.
  117.         joltages.Add(joltages[joltages.Count - 1] + 3);
  118.  
  119.     }
  120.  
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement