Exemplo n.º 1
0
        public void Control()
        {
            ManhattanLocation waypoint = new(10, 1);
            ManhattanLocation ship     = new();

            ship = FerryMover.MoveToWaypoint(waypoint, ship, 10);

            Assert.Equal(100, ship.HorizontalPosition);
            Assert.Equal(10, ship.VerticalPosition);

            waypoint.HorizontalPosition = 10;
            waypoint.VerticalPosition   = 1;

            waypoint = FerryMover.MoveDirectionally(waypoint, 'N', 3);

            Assert.Equal(10, waypoint.HorizontalPosition);
            Assert.Equal(4, waypoint.VerticalPosition);

            ship = FerryMover.MoveToWaypoint(waypoint, ship, 7);

            Assert.Equal(170, ship.HorizontalPosition);
            Assert.Equal(38, ship.VerticalPosition);

            waypoint = FerryMover.RotateWaypoint(waypoint, Directive.ROTATE_RIGHT, 90);

            Assert.Equal(4, waypoint.HorizontalPosition);
            Assert.Equal(-10, waypoint.VerticalPosition);

            ship = FerryMover.MoveToWaypoint(waypoint, ship, 11);

            Assert.Equal(214, ship.HorizontalPosition);
            Assert.Equal(-72, ship.VerticalPosition);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var instructions = FileHelper.GetFileContents <string>("./data/data.txt");

            int currentDirection       = Direction.EAST;
            ManhattanLocation position = new();

            foreach (string instruction in instructions)
            {
                char directive = instruction[0];
                int  amount    = int.Parse(instruction.Substring(1));

                // brute force it with a switch for now
                switch (directive)
                {
                case Directive.MOVE_NORTH:
                case Directive.MOVE_SOUTH:
                case Directive.MOVE_EAST:
                case Directive.MOVE_WEST:
                    position = FerryMover.MoveDirectionally(position, directive, amount);
                    break;

                case Directive.MOVE_FORWARD:
                    position = FerryMover.MoveForward(position, currentDirection, amount);
                    break;

                case Directive.ROTATE_RIGHT:
                case Directive.ROTATE_LEFT:
                    currentDirection = FerryMover.RotateShip(currentDirection, directive, amount);

                    break;

                default:
                    throw new Exception("Undefined directive");
                }
            }

            Console.WriteLine(Math.Abs(position.HorizontalPosition) + Math.Abs(position.VerticalPosition));
        }