Equd

AdventOfCode 2018 Day 09

Dec 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. void Main()
  2. {
  3.     ///TEST CODE
  4.     if(PlayGame(09, 0025) != 000032) throw new Exception("Failed Game");
  5.     if(PlayGame(10, 1618) != 008317) throw new Exception("Failed Game");
  6.     if(PlayGame(13, 7999) != 146373) throw new Exception("Failed Game");
  7.     if(PlayGame(17, 1104) != 002764) throw new Exception("Failed Game");
  8.     if(PlayGame(21, 6111) != 054718) throw new Exception("Failed Game");
  9.     if(PlayGame(30, 5807) != 037305) throw new Exception("Failed Game");
  10.    
  11.     //GET DATA
  12.     var aoc = new AdventOfCode(2018, 9);
  13.    
  14.     //PARSE IT
  15.     var i = Parse(aoc.InputLines[0]);
  16.    
  17.     //Submit
  18.     aoc.SubmitAnswer(PlayGame(i.playerCount, i.lastMarble), Part.A);
  19.     aoc.SubmitAnswer(PlayGame(i.playerCount, i.lastMarble * 100), Part.B);
  20. }
  21.  
  22. //Parse
  23. (int playerCount, int lastMarble) Parse(string line)
  24. {
  25.     var splt = line.Split(' ');
  26.     return(int.Parse(splt[0]), int.Parse(splt[6]));
  27. }
  28.  
  29.  
  30. long PlayGame(int playerCount, int lastMarble)
  31. {
  32.     //the players and their score
  33.     var players = new long[playerCount];
  34.     int playerPosition = 0;
  35.  
  36.     //circle with the marbles, small extension added to circular usage
  37.     var circle = new LinkedList<int>();
  38.  
  39.     //the currentMarble
  40.     int currentMarble = 0;
  41.    
  42.     //add the first
  43.     circle.AddFirst(currentMarble++);
  44.    
  45.     //the position we are working from
  46.     var position = circle.First;
  47.  
  48.     //loop till last marble
  49.     while (currentMarble <= lastMarble)
  50.     {
  51.         //move the playerposition by 1
  52.         playerPosition++;
  53.        
  54.         //special moment?
  55.         if (currentMarble % 23 == 0)
  56.         {
  57.             //add the score
  58.             players[playerPosition % playerCount] += currentMarble++;
  59.            
  60.             //move 7 times (bit ugly but save a for loop SPPEEED!
  61.             position = position.GetPrevious().GetPrevious().GetPrevious().GetPrevious().GetPrevious().GetPrevious().GetPrevious();
  62.            
  63.             //add the new current marble to the score
  64.             players[playerPosition % playerCount] += position.Value;
  65.            
  66.             //this needs to be removed
  67.             var toRemove = position;
  68.            
  69.             //get the new position
  70.             position = position.GetNext();
  71.            
  72.             //and remove
  73.             circle.Remove(toRemove);
  74.         }
  75.         else
  76.         {
  77.             //move
  78.             position = position.GetNext();
  79.            
  80.             //add marble
  81.             position = circle.AddAfter(position, currentMarble++);         
  82.         }
  83.     }  
  84.    
  85.     //return score
  86.     return players.Max();
  87. }
  88.  
  89. //extension, for easy circular list usage.
  90. public static class test
  91. {  
  92.     public static LinkedListNode<int> GetNext(this LinkedListNode<int> item)
  93.     {
  94.         if(item.List.Last == item) return item.List.First;
  95.         else return item.Next;         
  96.     }
  97.  
  98.     public static LinkedListNode<int> GetPrevious(this LinkedListNode<int> item)
  99.     {
  100.         if (item.List.First == item) return item.List.Last;
  101.         else return item.Previous;
  102.     }
  103. }
  104.  
  105. // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment