Last active
July 17, 2016 22:06
-
-
Save MartinBloedorn/4e421576112393004e3397b87771bee3 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 <stdio.h> | |
#include <stdlib.h> | |
int sgn(int x) { | |
return (x >= 0 ? 1 : -1); | |
} | |
int main(int argc, char * argv[]) { | |
if(argc != 3) { | |
printf("ERROR: Supply X and Y target position!\n"); | |
return -3; | |
} | |
int X = (int)atoi(argv[1]); | |
int Y = (int)atoi(argv[2]); | |
printf("Targets: X = %d and Y = %d\n", X, Y); | |
int cx, cy = 0; | |
int dx, dy, mx, my; | |
int moves = 0; | |
while(moves < 100000) { | |
moves++; | |
printf("Position: %d\t%d\n", cx, cy); | |
dx = X - cx; | |
dy = Y - cy; | |
mx = sgn(dx)*(dx % 2 < dy % 2? 2 : 1); | |
my = sgn(dy)*(abs(mx) == 2? 1 : 2); | |
// Pequena gambiarra. | |
if((abs(dx) == 2 && abs(dy) == 1) || (abs(dx) == 1 && abs(dy) == 2)) { | |
mx = dx; | |
my = dy; | |
} | |
cx = cx + mx; | |
cy = cy + my; | |
if(cx == X && cy == Y) { | |
printf("Got there in %d moves\n", moves); | |
return 1; | |
} | |
} | |
printf("Did not succeed...\n"); | |
return -2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment