Пример #1
0
        private static void Part2()
        {
            int wx = 10, wy = -1, x = 0, y = 0;

            foreach (var line in input)
            {
                char action = line[0];
                int  value  = int.Parse(line.Substring(1));

                switch (action)
                {
                case 'N': wy -= value; break;

                case 'S': wy += value; break;

                case 'E': wx += value; break;

                case 'W': wx -= value; break;

                case 'R':
                case 'L':
                    if ((action == 'R' && value == 90) || (action == 'L' && value == 270))
                    {
                        GenericExtensions.Swap(ref wx, ref wy);
                        wx *= -1;
                        break;
                    }

                    if ((action == 'R' && value == 270) || (action == 'L' && value == 90))
                    {
                        GenericExtensions.Swap(ref wx, ref wy);
                        wy *= -1;
                        break;
                    }

                    if (value == 180)
                    {
                        wx *= -1;
                        wy *= -1;
                        break;
                    }

                    break;

                case 'F':
                    // move to the waypoint the given number of times
                    x += (wx * value);
                    y += (wy * value);
                    break;

                default: break;
                }
            }

            Console.WriteLine($"\nPart 2: End at ({x},{y})");
            Console.WriteLine($"Manhattan Distance: {(x, y).ManhattanDistance((0, 0))}");
        }