Skip to content

Instantly share code, notes, and snippets.

@isopropylcyanide
Last active March 14, 2021 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isopropylcyanide/2fb85f848a9b0c8624c214a45a07e21f to your computer and use it in GitHub Desktop.
Save isopropylcyanide/2fb85f848a9b0c8624c214a45a07e21f to your computer and use it in GitHub Desktop.
#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