mastersplinter19

AOC2021_06_2

Dec 6th, 2021 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. readonly static string _puzzle = "06";
  2. static List<int> _input = File.ReadAllText(Util.CurrentQuery.Location + $"\\{_puzzle}.txt").Split(',').Select(f => int.Parse(f)).ToList();
  3. static int _targetDays = 256;
  4.  
  5. static ulong _lanternfishCount = 0;
  6. static ulong _initialStateLanternfishCount = 0;
  7.  
  8. Dictionary<int, ulong> _initialStateLanternfish = new Dictionary<int, ulong>();
  9. Dictionary<int, int> _initialStateCount = new Dictionary<int, int>();
  10.  
  11. void Main()
  12. {
  13.     //_input = File.ReadAllText(Util.CurrentQuery.Location + $"\\{_puzzle}_test.txt").Split(',').Select(f => int.Parse(f)).ToList();
  14.    
  15.     // calculate via divide and modulo how many fish are available after 80 days
  16.    
  17.     // for a set of fish, how many fish do they each create over _targetDays?
  18.     // how many do that set create with their _targetDays?
  19.     // This is a recursive function
  20.    
  21.     foreach (var i in _input)
  22.     {
  23.         // Oh, yeah, use a dictionary so we don't have to recalculate for each initial state in the input. /facepalm
  24.         if (_initialStateLanternfish.ContainsKey(i))
  25.         {
  26.             _lanternfishCount += _initialStateLanternfish[i];
  27.         }
  28.         else
  29.         {
  30.             GenerateLanternfish(_targetDays, i);
  31.             _initialStateLanternfishCount++;
  32.  
  33.             _lanternfishCount += _initialStateLanternfishCount;
  34.             _initialStateLanternfish.Add(i, _initialStateLanternfishCount);
  35.             _initialStateLanternfishCount = 0;
  36.         }
  37.     }
  38.    
  39.     //_lanternfish.Dump();
  40.    
  41.     _lanternfishCount.Dump();
  42.     // 1639598111948
  43. }
  44.  
  45. // Define other methods and classes here
  46. void GenerateLanternfish(int targetDays, int initialState)
  47. {
  48.     for (var i = targetDays - initialState; i > 0; i -= 7)
  49.     {
  50.         GenerateLanternfish(i, 9);
  51.         _initialStateLanternfishCount++;
  52.     }
  53. }
Add Comment
Please, Sign In to add comment