2
* @param {number[]} commands3
* @param {number[][]} obstacles6
var robotSim = function (commands, obstacles) {8
let currentPosition = [0, 0];9
let currentDirection = "top";10
const mySet = new Set();12
// This is to have a O(1) check instead of doing a linear scan13
obstacles.forEach((obs) => {14
mySet.add(`${obs[0]},${obs[1]}`);17
for (let i = 0; i < commands.length; i++) {19
const move = commands[i];21
if (currentDirection === "top") {22
currentDirection = "right";23
} else if (currentDirection === "right") {24
currentDirection = "down";25
} else if (currentDirection === "down") {26
currentDirection = "left";28
currentDirection = "top";33
if (currentDirection === "top") {34
currentDirection = "left";35
} else if (currentDirection === "left") {36
currentDirection = "down";37
} else if (currentDirection === "down") {38
currentDirection = "right";40
currentDirection = "top";45
for (let i = 0; i < move; i++) {47
const isY = currentDirection === "top" || currentDirection === "down";49
currentDirection === "top" || currentDirection === "right";52
currentPosition[0] + (!isY ? (isPositive ? 1 : -1) : 0),53
currentPosition[1] + (isY ? (isPositive ? 1 : -1) : 0),55
const positionString = `${newPosition[0]},${newPosition[1]}`;57
if (!mySet.has(positionString)) {58
currentPosition = newPosition;61
Math.pow(currentPosition[0], 2) + Math.pow(currentPosition[1], 2)