Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- #region input
- var testInputA = new string[]
- {
- @"/->-\ ",
- @"| | /----\",
- @"| /-+--+-\ |",
- @"| | | | v |",
- @"\-+-/ \-+--/",
- @" \------/ "
- };
- var testInputB = new string[]
- {
- @"/>-<\ ",
- @"| | ",
- @"| /<+-\",
- @"| | | v",
- @"\>+</ |",
- @" | ^",
- @" \<->/",
- };
- #endregion
- //tests
- if(AnswerA(testInputA, Part.A) != "7,3") throw new Exception("failed test a1");
- if(AnswerA(testInputB, Part.B) != "6,4") throw new Exception("failed test a2");
- var aoc = new AdventOfCode(2018, 13);
- aoc.SubmitAnswer(AnswerA(aoc.InputLines, Part.A), Part.A);
- aoc.SubmitAnswer(AnswerA(aoc.InputLines, Part.B), Part.B);
- }
- class Cart
- {
- //order of turns
- private static Direction[] intersectionOrder = new[] { Direction.Left, Direction.Straight, Direction.Right, };
- //current position
- public Position<char> position;
- //still oke
- public bool Crashed = false;
- //position
- public int x => position.x;
- public int y => position.y;
- //nextDirection
- private int moveDirection;
- //constructor
- public Cart(int index, Position<char> pos)
- {
- this.position = pos;
- switch (pos.Value)
- {
- case '>': this.position.currentDirection = Direction.East; break;
- case '<': this.position.currentDirection = Direction.West; break;
- case '^': this.position.currentDirection = Direction.North; break;
- case 'v': this.position.currentDirection = Direction.South; break;
- default: throw new NotImplementedException();
- }
- }
- //move
- public void Move()
- {
- //determine direction
- if (this.position.Value == '\\')
- {
- if (this.position.currentDirection == Direction.North || this.position.currentDirection == Direction.South)
- this.position = this.position.Move(Direction.Left);
- else if (this.position.currentDirection == Direction.East || this.position.currentDirection == Direction.West)
- this.position = this.position.Move(Direction.Right);
- }
- else if (this.position.Value == '/')
- {
- if (this.position.currentDirection == Direction.North || this.position.currentDirection == Direction.South)
- this.position = this.position.Move(Direction.Right);
- else if (this.position.currentDirection == Direction.East ||this.position.currentDirection == Direction.West)
- this.position = this.position.Move(Direction.Left);
- }
- //turn
- else if (this.position.Value == '+')
- {
- this.position = this.position.Move(intersectionOrder[moveDirection % 3]);
- moveDirection++;
- }
- //just forward
- else
- this.position = this.position.Move();
- }
- }
- public string AnswerA(string[] input, Part part)
- {
- //create mine
- var mine = Arr2D<char>.FromLines(input);
- //find the starting positions
- var carts = mine.Find(new char[] { '>', '<', '^', 'v' }).Select((x, i) => new Cart(i, x)).ToArray();
- //ui stuff
- //var dc = new DumpContainer().Dump();
- do
- {
- //dcCycle.UpdateContent($"CYCLE: {cycle:D4} \t CARTS: {carts.Length:D2}");
- //dc.UpdateContent(Util.RawHtml(grid.ToHTML(carts.Select(x => x.position))));
- for (int i = 0; i < carts.Length; i++)
- {
- //move each cart
- carts[i].Move();
- //did I move into an occupied space?
- for (int f = 0; f < carts.Length; f++)
- {
- if (i == f) continue; //same cart
- if (carts[i].position.DoesItOverlap(carts[f].position)) //collision
- {
- if (part == Part.A)
- {
- return $"{carts[i].x},{carts[i].y}"; //done
- }
- else
- {
- //set crashed
- carts[i].Crashed = true;
- carts[f].Crashed = true;
- }
- }
- }
- }
- //reorder and remove crashed
- carts = carts.OrderBy(x => x.y).ThenBy(x => x.x).Where(x => x.Crashed == false).ToArray();
- }while (carts.Length > 1);
- //show last cart
- return $"{carts[0].x},{carts[0].y}";
- }
Add Comment
Please, Sign In to add comment