Skip to content

Instantly share code, notes, and snippets.

@heimskr
Created March 3, 2021 13:15
Show Gist options
  • Save heimskr/9368b262c7794eb9199bed64d5cd0226 to your computer and use it in GitHub Desktop.
Save heimskr/9368b262c7794eb9199bed64d5cd0226 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
int main() {
int x = 0, y = 0;
enum Direction {Up, Right, Down, Left};
Direction direction = Up;
int max_extent = 1;
int extent = 0;
std::map<Direction, const char *> dirs {{Up, "up"}, {Right, "right"}, {Down, "down"}, {Left, "left"}};
for (int i = 0; i < 64; ++i) {
switch (direction) {
case Up: --y; break;
case Right: ++x; break;
case Down: ++y; break;
case Left: --x; break;
}
std::cout << x << ", " << y << " " << dirs[direction] << "\n";
if (++extent == max_extent) {
extent = 0;
switch (direction) {
case Up: direction = Right; break;
case Right: direction = Down; ++max_extent; break;
case Down: direction = Left; break;
case Left: direction = Up; ++max_extent; break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment