Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- //Advent of Code 2020 Day 10 Parts 1 and 2 solution by Mike LeSauvage
- public class AdapterAnalysis : MonoBehaviour
- {
- [SerializeField] TextAsset joltageTextfile = null; //Hooked up in the input text in the Unity editor.
- List<int> joltages = new List<int>();
- void Start()
- {
- string[] joltageStrings = joltageTextfile.text.Split('\n');
- PrepareJoltages(joltageStrings); //Adds the starting joltage, adapter joltage, and sorts them.
- SolvePartOne();
- SolvePartTwo();
- }
- void SolvePartTwo()
- {
- //Get list of length of all continguous 1-jolt adapters gaps.
- //The adapters on the ends are not counted as they are required to be in the sequence to support their 3-jolt gap.
- //Eg: 0 3 4 5 6 7 8 11 -> count is 5 (4-8)
- // 0 3 4 5 8 -> count is 1 (4)
- // 0 3 4 5 6 9 -> count is 2 (4-6)
- List<int> oneJoltRunLengths = new List<int>();
- int contiguousCount = 0;
- for(int i=0; i<joltages.Count-1; i++)
- {
- if(joltages[i+1] - joltages[i] == 1)
- {
- contiguousCount++;
- }
- else
- {
- contiguousCount--;
- if (contiguousCount >= 1)
- {
- oneJoltRunLengths.Add(contiguousCount);
- }
- contiguousCount = 0;
- }
- }
- //Getting the total number of combinations means finding how many ways in which adapters can be left unused from sequence
- //Adapters can be left out as long as they don't create a three-jolt gap.
- //The list of oneJoltRunLengths has been prepared as counts of
- // 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
- // combinations are there for a run of 1-jolt adapter gaps that don't create a gap of 3 or more.
- //For a single adapter, there are two ways. (0, 1)
- //For two adapters in a row, there are four. (00, 01, 10, 11)
- //For three, there are seven ways (001, 010, 011, 100, 101, 110, 111 -> just not 000)
- //Past that point, this approach can be used:
- // https://math.stackexchange.com/questions/2844818/coin-tossing-problem-where-three-tails-come-in-a-row
- //
- //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
- // array runCombinations rather than a generalized solution.
- //To get the total combination, multiply the number of combinations of each run by each other!
- long totalCombinations = 1;
- int[] runCombinations = { 1, 2, 4, 7 };
- foreach(int c in oneJoltRunLengths)
- {
- totalCombinations *= runCombinations[c];
- }
- Debug.Log($"Total number of adapter combinations: {totalCombinations}");
- }
- void SolvePartOne()
- {
- int[] joltageRangeCounts = new int[3]; //Stores the histogram of joltages for gaps of 1,2, and 3 (in positions 0,1,2)
- GenerateJoltageHistogram(joltageRangeCounts);
- int oneJoltDifferences = joltageRangeCounts[0];
- int threeJoltDifferences = joltageRangeCounts[2];
- Debug.Log($"1-jolt Differences: {oneJoltDifferences}");
- Debug.Log($"3-jolt Differences: {threeJoltDifferences}");
- Debug.Log($"1-jolt differences multiplied by 3-jolt differences: { oneJoltDifferences * threeJoltDifferences}");
- }
- //Creates bins of joltage gaps (1, 2, and 3).
- //Assumes there are adapters to span all gaps (no 4-jolt and larger gaps!)
- void GenerateJoltageHistogram(int[] joltageRangeCounts)
- {
- for (int i = 0; i < joltages.Count - 1; i++)
- {
- int joltageRange = joltages[i + 1] - joltages[i];
- joltageRangeCounts[joltageRange - 1]++;
- }
- }
- //Add in the zero-joltage start point and the device's built-in 3-jolt adapater.
- //Sorts the list from smallest to largest.
- void PrepareJoltages(string[] joltageStrings)
- {
- //Seed with the outlet joltage.
- joltages.Add(0);
- foreach (string s in joltageStrings)
- {
- joltages.Add(int.Parse(s));
- }
- joltages.Sort();
- //Add the device's joltage to the end.
- joltages.Add(joltages[joltages.Count - 1] + 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement