public static GridPoint MoveRobot( GridPoint initialPoint, RobotDirection robotDirection) { if (RobotDirection.Left.Equals(robotDirection)) { return(initialPoint.MoveLeft(1)); } else if (RobotDirection.Right.Equals(robotDirection)) { return(initialPoint.MoveRight(1)); } else if (RobotDirection.Up.Equals(robotDirection)) { return(initialPoint.MoveUp(1)); } else if (RobotDirection.Down.Equals(robotDirection)) { return(initialPoint.MoveDown(1)); } else { throw new Exception($"Invalid robot direction {robotDirection}"); } }
public static bool GetIsBoxInsideBeam( BigInteger[] program, GridPoint topLeft, int width, int height, bool isTopLeftAlreadyScanned) { var boxCorners = new List <GridPoint>() { topLeft.MoveRight(width - 1), topLeft.MoveUp(height - 1) }; if (!isTopLeftAlreadyScanned) { boxCorners.Add(topLeft); } foreach (var corner in boxCorners) { var scanResult = ScanPoint(program, corner); if (!DroneStatus.Pulled.Equals(scanResult)) { return(false); } } return(true); }
public static GridPoint GetNextPoint( GridPoint robotPosition, int movementCommand) { // Only four movement commands are understood: north (1), // south (2), west (3), and east (4). Any other command is invalid. // The movements differ in direction, but not in distance: in a // long enough east-west hallway, a series of commands like // 4,4,4,4,3,3,3,3 would leave the repair droid back where it // started. if (movementCommand == 1) { return(robotPosition.MoveUp(1)); } else if (movementCommand == 2) { return(robotPosition.MoveDown(1)); } else if (movementCommand == 3) { return(robotPosition.MoveLeft(1)); } else if (movementCommand == 4) { return(robotPosition.MoveRight(1)); } else { throw new Exception($"Invalid movement command {movementCommand}"); } }
public static IList <GridPoint> GetAdjacentPoints(GridPoint point) { var result = new List <GridPoint>() { point.MoveLeft(1), point.MoveRight(1), point.MoveUp(1), point.MoveDown(1) }; return(result); }
public static GridPoint[] GeneratePath(string[] instructions) { // Initialize the path with the origin var currentPoint = new GridPoint(); var path = new List <GridPoint>(new GridPoint[] { currentPoint }); // Generate the path by reading each instruction and moving the // current point to the next point as instructed foreach (var rawInstruction in instructions) { // The instruction should be of the following pattern: // 1) L/R/U/D for the direction // 2) A number indicating how far to move var instruction = rawInstruction.Trim(); var match = Regex.Match(instruction, "^(L|R|U|D)([0-9]+)$"); if (!match.Success) { throw new Exception($"Invalid instruction: {instruction}"); } var direction = match.Groups[1].Value; var distance = int.Parse(match.Groups[2].Value); for (int i = 0; i < distance; i++) { if ("L".Equals(direction)) { currentPoint = currentPoint.MoveLeft(1); } else if ("R".Equals(direction)) { currentPoint = currentPoint.MoveRight(1); } else if ("U".Equals(direction)) { currentPoint = currentPoint.MoveUp(1); } else if ("D".Equals(direction)) { currentPoint = currentPoint.MoveDown(1); } path.Add(currentPoint); } } return(path.ToArray()); }
public IList <GridPoint> GetPointNeighbors( GridPoint point, SortedDictionary <string, string> keysCollected, GridPoint specificNeighborToAllow = null, bool ignoreDoors = false) { var neighbors = new List <GridPoint>(); var candidates = new List <GridPoint>() { point.MoveLeft(1), point.MoveRight(1), point.MoveUp(1), point.MoveDown(1) }; foreach (var candidate in candidates) { if (!MazeCells.ContainsKey(candidate)) { continue; } var candidateCell = MazeCells[candidate]; bool checkIfCanCenterCell = !( specificNeighborToAllow != null && specificNeighborToAllow.Equals(candidate)); if (checkIfCanCenterCell) { if (!GetCanEnterCell(candidate, keysCollected, ignoreDoors)) { continue; } } neighbors.Add(candidate); } return(neighbors); }