Equd

AdventOfCode 2018 Day 14

Dec 14th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. void Main()
  2. {
  3.     #region input
  4.     var testInputA = new string[]
  5.     {
  6.         @"/->-\        ",
  7.         @"|   |  /----\",
  8.         @"| /-+--+-\  |",
  9.         @"| | |  | v  |",
  10.         @"\-+-/  \-+--/",
  11.         @"  \------/   "
  12.     };
  13.  
  14.     var testInputB = new string[]
  15.     {
  16.         @"/>-<\  ",
  17.         @"|   |  ",
  18.         @"| /<+-\",
  19.         @"| | | v",
  20.         @"\>+</ |",
  21.         @"  |   ^",
  22.         @"  \<->/",
  23.     };
  24.     #endregion
  25.    
  26.     //tests
  27.     if(AnswerA(testInputA, Part.A) != "7,3") throw new Exception("failed test a1");
  28.     if(AnswerA(testInputB, Part.B) != "6,4") throw new Exception("failed test a2");
  29.    
  30.     var aoc = new AdventOfCode(2018, 13);
  31.     aoc.SubmitAnswer(AnswerA(aoc.InputLines, Part.A), Part.A); 
  32.     aoc.SubmitAnswer(AnswerA(aoc.InputLines, Part.B), Part.B);
  33. }
  34.  
  35. class Cart
  36. {
  37.     //order of turns
  38.     private static Direction[] intersectionOrder = new[] { Direction.Left, Direction.Straight, Direction.Right, };
  39.    
  40.     //current position
  41.     public Position<char> position;
  42.    
  43.     //still oke
  44.     public bool Crashed = false;
  45.    
  46.     //position
  47.     public int x => position.x;
  48.     public int y => position.y;
  49.    
  50.     //nextDirection
  51.     private int moveDirection;
  52.    
  53.     //constructor
  54.     public Cart(int index, Position<char> pos)
  55.     {
  56.         this.position = pos;
  57.         switch (pos.Value)
  58.         {
  59.             case '>': this.position.currentDirection = Direction.East; break;
  60.             case '<': this.position.currentDirection = Direction.West; break;
  61.             case '^': this.position.currentDirection = Direction.North; break;
  62.             case 'v': this.position.currentDirection = Direction.South; break;
  63.             default: throw new NotImplementedException();
  64.         }
  65.     }
  66.  
  67.     //move
  68.     public void Move()
  69.     {
  70.         //determine direction
  71.         if (this.position.Value == '\\')
  72.         {
  73.             if (this.position.currentDirection == Direction.North || this.position.currentDirection == Direction.South)
  74.                 this.position = this.position.Move(Direction.Left);
  75.             else if (this.position.currentDirection == Direction.East || this.position.currentDirection == Direction.West)
  76.                 this.position = this.position.Move(Direction.Right);
  77.         }
  78.         else if (this.position.Value == '/')
  79.         {
  80.             if (this.position.currentDirection == Direction.North || this.position.currentDirection == Direction.South)
  81.                 this.position = this.position.Move(Direction.Right);
  82.             else if (this.position.currentDirection == Direction.East ||this.position.currentDirection == Direction.West)
  83.                 this.position = this.position.Move(Direction.Left);
  84.         }
  85.         //turn
  86.         else if (this.position.Value == '+')
  87.         {
  88.             this.position = this.position.Move(intersectionOrder[moveDirection % 3]);
  89.             moveDirection++;                       
  90.         }
  91.         //just forward
  92.         else
  93.             this.position = this.position.Move();
  94.  
  95.     }
  96. }
  97.  
  98.  
  99. public string AnswerA(string[] input, Part part)
  100. {      
  101.     //create mine
  102.     var mine = Arr2D<char>.FromLines(input);
  103.  
  104.     //find the starting positions
  105.     var carts = mine.Find(new char[] { '>', '<', '^', 'v' }).Select((x, i) => new Cart(i, x)).ToArray();       
  106.  
  107.     //ui stuff
  108.     //var dc = new DumpContainer().Dump();
  109.    
  110.     do
  111.     {              
  112.         //dcCycle.UpdateContent($"CYCLE: {cycle:D4} \t CARTS: {carts.Length:D2}");
  113.         //dc.UpdateContent(Util.RawHtml(grid.ToHTML(carts.Select(x => x.position))));      
  114.                
  115.         for (int i = 0; i < carts.Length; i++)
  116.         {
  117.             //move each cart
  118.             carts[i].Move();
  119.  
  120.             //did I move into an occupied space?
  121.             for (int f = 0; f < carts.Length; f++)
  122.             {
  123.                 if (i == f) continue; //same cart
  124.                 if (carts[i].position.DoesItOverlap(carts[f].position)) //collision
  125.                 {
  126.                     if (part == Part.A)
  127.                     {
  128.                         return $"{carts[i].x},{carts[i].y}"; //done
  129.                     }
  130.                     else
  131.                     {
  132.                         //set crashed
  133.                         carts[i].Crashed = true;
  134.                         carts[f].Crashed = true;                       
  135.                     }
  136.                 }
  137.             }          
  138.         }      
  139.         //reorder and remove crashed
  140.         carts = carts.OrderBy(x => x.y).ThenBy(x => x.x).Where(x => x.Crashed == false).ToArray();
  141.     }while (carts.Length > 1);
  142.    
  143.     //show last cart
  144.     return $"{carts[0].x},{carts[0].y}";
  145. }
Add Comment
Please, Sign In to add comment