Last active
March 14, 2021 10:42
-
-
Save isopropylcyanide/2fb85f848a9b0c8624c214a45a07e21f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
enum direction { | |
N, S, W, E | |
}; | |
//pre compute where robot will move when he moves left from a fixed direction | |
map<direction, direction> leftDir = { | |
{N, W}, {W, S}, {S, E}, {E, N}}; | |
//pre compute where robot will move when he moves right from a fixed direction | |
map<direction, direction> rightDir = { | |
{N, E}, {E, S}, {S, W}, {W, N}}; | |
bool doesCircleExist(string instructions) { | |
direction dir = N; | |
//initial coordinates | |
int x = 0, y = 0; | |
for (char ch : instructions) { | |
if (ch == 'G') { | |
if (dir == N || dir == S) { | |
//move north or south | |
y = dir == N ? y + 1 : y - 1; | |
} else if (dir == W || dir == E) { | |
//move west or east | |
x = dir == W ? x - 1 : x + 1; | |
} | |
} else if (ch == 'L') { | |
dir = leftDir[dir]; | |
} else { | |
dir = rightDir[dir]; | |
} | |
} | |
//at origin and not north | |
if ((x == 0 && y == 0) || (dir != N)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment