Exemple #1
0
            private bool findInDirection(Direction direction, Coordinate origo, Dictionary<Coordinate, Seat> seats)
            {
                var position = new Coordinate { X = origo.X, Y = origo.Y };
                Seat seat;
                var outOfBounds = false;
                do
                {
                    position = CoordinateMovement.Move(position, direction);
                    outOfBounds = !(position.X >= 0 && position.X <= maxX && position.Y <= 0 && position.Y >= maxY);
                    seats.TryGetValue(position, out seat);
                } while (seat == null && !outOfBounds);

                return seat != null && seat.Status == SeatStatus.occupied;
            }
            public int RunChallenge(List <string> instructions)
            {
                var ship = new Ship();

                foreach (var instruction in instructions)
                {
                    var value = int.Parse(instruction.Substring(1));
                    switch (instruction[0])
                    {
                    case 'N':
                        ship.Position.Y += value;
                        break;

                    case 'S':
                        ship.Position.Y -= value;
                        break;

                    case 'E':
                        ship.Position.X += value;
                        break;

                    case 'W':
                        ship.Position.X -= value;
                        break;

                    case 'F':
                        for (int i = 0; i < value; i++)
                        {
                            ship.Position = CoordinateMovement.Move(ship.Position, ship.Direction);
                        }
                        break;

                    case 'L':
                        for (int i = 0; i < value / 90; i++)
                        {
                            ship.Direction = DirectionTurns.TurnLeft(ship.Direction);
                        }
                        break;

                    case 'R':
                        for (int i = 0; i < value / 90; i++)
                        {
                            ship.Direction = DirectionTurns.TurnRight(ship.Direction);
                        }
                        break;
                    }
                }
                return(Math.Abs(ship.Position.X) + Math.Abs(ship.Position.Y));
            }