Beispiel #1
0
        public void SolvePart1()
        {
            Coord coord = new Coord(0, 0);

            string[] directions  = InputParser.ReadToStringArr(Day);
            char     orientation = 'E';

            foreach (string direction in directions)
            {
                (char dir, int val) = DirUtils.ParseDir(direction);

                if (DirUtils.CardinalKeys.Contains(dir))
                {
                    CardinalDir step = DirUtils.dirLkup[dir];
                    coord.X += (val * step.dx);
                    coord.Y += (val * step.dy);
                }
                else if (DirUtils.Turns.Contains(dir))
                {
                    for (int i = 0; i < (val / 90); i++)
                    {
                        orientation = DirUtils.turnLkp[dir][orientation];
                    }
                }
                else if (dir == 'F')
                {
                    CardinalDir step = DirUtils.dirLkup[orientation];
                    coord.X += (val * step.dx);
                    coord.Y += (val * step.dy);
                }
            }
            int solution = Math.Abs(coord.X) + Math.Abs(coord.Y);

            Console.WriteLine($"Manhattan Distance: {solution}");
        }
Beispiel #2
0
        public void SolveTest()
        {
            Coord shipLoc  = new Coord(0, 0);
            Coord waypoint = new Coord(1, 10);

            string[] directions  = InputParser.ReadToStringArr(Day, "Test");
            char     orientation = 'E';

            foreach (string direction in directions)
            {
                (char dir, int val) = DirUtils.ParseDir(direction);

                if (DirUtils.CardinalKeys.Contains(dir))
                {
                    CardinalDir step = DirUtils.dirLkup[dir];
                    waypoint.X += (val * step.dx);
                    waypoint.Y += (val * step.dy);
                }
                else if (DirUtils.Turns.Contains(dir))
                {
                    for (int i = 0; i < (val / 90); i++)
                    {
                        if (dir == 'R')
                        {
                            int x = waypoint.X;
                            int y = waypoint.Y;
                            waypoint.X = y;
                            waypoint.Y = -x;
                        }
                        if (dir == 'L')
                        {
                            int x = waypoint.X;
                            int y = waypoint.Y;
                            waypoint.X = -y;
                            waypoint.Y = x;
                        }
                    }
                }
                else if (dir == 'F')
                {
                    CardinalDir step = DirUtils.dirLkup[orientation];
                    for (int i = 0; i < val; i++)
                    {
                        shipLoc.X += waypoint.X;
                        shipLoc.Y += waypoint.Y;
                    }
                }
            }
            int solution = Math.Abs(shipLoc.X) + Math.Abs(shipLoc.Y);

            Console.WriteLine($"Manhattan Distance: {solution}");
        }