Equd

AdventOfCode 2018 Day 12

Dec 12th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. void Main()
  2. {
  3.     #region testdata
  4.     var test = new string[] {
  5.     "#..#.#..##......###...###...........",
  6.     "",
  7.     "...## => #",
  8.     "..#.. => #",
  9.     ".#... => #",
  10.     ".#.#. => #",
  11.     ".#.## => #",
  12.     ".##.. => #",
  13.     ".#### => #",
  14.     "#.#.# => #",
  15.     "#.### => #",
  16.     "##.#. => #",
  17.     "##.## => #",
  18.     "###.. => #",
  19.     "###.# => #",
  20.     "####. => #",
  21.     };
  22.     #endregion
  23.  
  24.     //test
  25.     if(answerA(test, 20) != 325) throw new Exception("failed test a");
  26.    
  27.     var aoc = new AdventOfCode(2018, 12);
  28.     aoc.SubmitAnswer(answerA(aoc.InputLines, 20), Part.A);
  29.     aoc.SubmitAnswer(answerA(aoc.InputLines, 50000000000), Part.B);
  30. }
  31.  
  32.  
  33. public long answerA(string[] input, long cycles)
  34. {
  35.     //get input
  36.     string state = input[0].Replace("initial state: ", "").Trim();
  37.  
  38.     //create rule dic
  39.     var dicRules = input.Skip(2).Select(x => x.Split(' ')).ToDictionary(x=> x[0], x => x[2]);
  40.    
  41.     //determine leftsize added
  42.     long appendLeft = 0;           
  43.    
  44.     //each cycle
  45.     for(int i = 0; i < cycles; i++)
  46.     {
  47.         //get next state
  48.         var nextGen = NextGen(state, appendLeft, dicRules);
  49.  
  50.         //did it change?
  51.         if(nextGen.state == state)     
  52.         {  
  53.             //nope, what the next state
  54.             var nextGen2 = NextGen(nextGen.state, nextGen.appendLeft, dicRules);
  55.            
  56.             //determine the delta per cycle
  57.             long delta = nextGen2.appendLeft - nextGen.appendLeft;
  58.            
  59.             //move append forward in time
  60.             appendLeft += (cycles - i) * delta;
  61.             break; //DONE!
  62.         }
  63.         else
  64.         {
  65.             //switch
  66.             state = nextGen.state;
  67.             appendLeft = nextGen.appendLeft;
  68.         }
  69.     }
  70.        
  71.     //sum
  72.     long sum = 0;
  73.    
  74.     //loop through the plants
  75.     for(int i = 0; i < state.Length; i++)
  76.     {
  77.         //add score
  78.         if(state[i] == '#') sum += i - appendLeft;
  79.     }
  80.     return sum;
  81. }
  82.  
  83. public (string state, long appendLeft) NextGen(string state, long appendLeft, Dictionary<string, string> dicRules)
  84. {
  85.     //append start if needed
  86.     while (state.StartsWith("...") == false)
  87.     {
  88.         state = "." + state;
  89.         appendLeft++;
  90.     }
  91.     //append end if needed
  92.     while (state.EndsWith("...") == false)
  93.     {
  94.         state = state + ".";
  95.     }
  96.  
  97.     //build the new state
  98.     StringBuilder sb = new StringBuilder();
  99.  
  100.     //loop through the state
  101.     for (int x = 0; x < state.Length - 4; x++)
  102.     {
  103.         if (dicRules.TryGetValue(state.Substring(x, 5), out string c))
  104.             sb.Append(c);
  105.         else sb.Append(".");
  106.     }
  107.  
  108.     //create the new state (.. is default as those fall off per rules
  109.     state = sb.ToString() + "..";
  110.     appendLeft -= 2;
  111.    
  112.     //done
  113.     return(state, appendLeft);
  114. }
  115.  
  116. // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment