Skip to content

Commit

Permalink
green: rotates left and move forward (gets out of grid)
Browse files Browse the repository at this point in the history
  • Loading branch information
acidfernando committed Mar 15, 2024
1 parent fbb7ee8 commit 1177810
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/MarsRover.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
export class MarsRover {

private facingDirection: string = 'N'
private facingDirection: string = "N"
private xPosition: number = 0
private yPosition: number = 0

constructor() {}

private moveLeft() {
if (this.xPosition === 0) {
this.xPosition = 9
return
}

this.xPosition -= 1
}

public execute(command: string): string {

command.split('').forEach((instruction) => {
switch(instruction) {
case 'M':
if (this.facingDirection == 'N') this.yPosition++
else if (this.facingDirection == 'E') this.xPosition++
else if (this.facingDirection == 'S') this.yPosition--
else if (this.facingDirection == 'W') this.xPosition--
break;
case 'L':
if (this.facingDirection == 'N') this.facingDirection = 'W'
else if (this.facingDirection == 'E') this.facingDirection = 'N'
else if (this.facingDirection == 'S') this.facingDirection = 'E'
else if (this.facingDirection == 'W') this.facingDirection = 'S'
break;
case 'R':
if (this.facingDirection == 'N') this.facingDirection = 'E'
else if (this.facingDirection == 'E') this.facingDirection = 'S'
else if (this.facingDirection == 'S') this.facingDirection = 'W'
else if (this.facingDirection == 'W') this.facingDirection = 'N'
break;
command.split("").forEach((instruction) => {
switch (instruction) {
case "M":
if (this.facingDirection == "N") this.yPosition++
else if (this.facingDirection == "E") this.xPosition++
else if (this.facingDirection == "S") this.yPosition--
else if (this.facingDirection == "W") this.moveLeft()
break
case "L":
if (this.facingDirection == "N") this.facingDirection = "W"
else if (this.facingDirection == "E") this.facingDirection = "N"
else if (this.facingDirection == "S") this.facingDirection = "E"
else if (this.facingDirection == "W") this.facingDirection = "S"
break
case "R":
if (this.facingDirection == "N") this.facingDirection = "E"
else if (this.facingDirection == "E") this.facingDirection = "S"
else if (this.facingDirection == "S") this.facingDirection = "W"
else if (this.facingDirection == "W") this.facingDirection = "N"
break
}
})

return `${this.xPosition}:${this.yPosition}:${this.facingDirection}`
}
}

0 comments on commit 1177810

Please sign in to comment.