Advertisement
eltea

Advent of Code 2020 Day 15 Parts 1 and 2

Dec 15th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. //Advent of Code 2020 Day 15 Parts 1 and 2 solution by Mike LeSauvage
  2.  
  3. using System.Collections;
  4. using UnityEngine;
  5. using System.Linq;
  6.  
  7. public class MemoryGame : MonoBehaviour
  8. {
  9.     //Store the spoken numbers in a hashtable.
  10.     //Format:
  11.     //  - Each entry in the hash is a 2-element array storing the last two numbers that were spoken.
  12.     //  - When the number hasn't been spoken, there is no entry for that number in the hashtable.
  13.     //  - When first spoken, the array is initialized to [-1, turn spoken].
  14.     //  - When spoken again, older turns are pushed to the left.
  15.     Hashtable spokenNumbers = new Hashtable();
  16.     int numTurns = 30000000;
  17.     int[] puzzleInputSeedNumbers = { 0, 5, 4, 1, 10, 14, 7 };
  18.     int[] testSeedNumbers = { 0, 3, 6 };
  19.  
  20.     //Start is part of Unity; put code where it makes sense for you!
  21.     void Start()
  22.     {
  23.         int[] seedNumbers = puzzleInputSeedNumbers;
  24.         int turn = 1;
  25.  
  26.         //Speak the seed numbers.
  27.         //NOTE: This approach assumes seed numbers are not duplicated! (Might work but not considered/tested)
  28.         for(; turn<=seedNumbers.Length; turn++)
  29.         {
  30.             SpeakNumber(seedNumbers[turn - 1], turn);
  31.         }
  32.  
  33.         int prevSpoken = seedNumbers.Last();
  34.  
  35.         //Iterate through to the final number.
  36.         for (; turn <= numTurns; turn++)
  37.         {
  38.             int speak = GetTurnSinceLastSpoken(prevSpoken, turn);
  39.             SpeakNumber(speak, turn);
  40.             prevSpoken = speak;
  41.         }
  42.  
  43.         Debug.Log($"Last spoken on turn {turn-1} was {prevSpoken}");
  44.     }
  45.  
  46.     int GetTurnSinceLastSpoken(int num, int currentTurn)
  47.     {
  48.         int[] turnsSpoken = (int[])spokenNumbers[num];  //Get the 2D array from the hash. Entry is expected to exist!
  49.  
  50.         //Only one in the list! Was first spoken last turn
  51.         if (turnsSpoken[0] == -1)
  52.         {
  53.             return 0;
  54.         }
  55.  
  56.         //Two in the list. [1] has the entry from last turn... look at [0] for the previous entry.
  57.         int lastTurnSpoken = turnsSpoken[0];
  58.         return currentTurn-1 - lastTurnSpoken; //Extra -1 as we're considering the prevous turn as the starting point.
  59.     }
  60.  
  61.     //If the number hasn't been spoken before, creates a hash entry for it as
  62.     //a 2D array and seeds it with [-1, currentTurn].
  63.     //It it has been spoken before, moves the recent entry to [0] and adds
  64.     //currentTurn to [1] so the most recent turn the number was spoken is at [1].
  65.     void SpeakNumber(int num, int currentTurn)
  66.     {
  67.         int[] turnsSpoken = (int[])spokenNumbers[num];
  68.  
  69.         if (turnsSpoken == null)
  70.         {
  71.             turnsSpoken = new int[2] { -1, currentTurn };
  72.             spokenNumbers[num] = turnsSpoken;
  73.         }
  74.         else
  75.         {
  76.             turnsSpoken[0] = turnsSpoken[1];
  77.             turnsSpoken[1] = currentTurn;
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement