Skip to main content
6 of 13
-5, assuming d=0 - an assumption that holds in /my/ Delphi ;-)
PatrickvL
  • 691
  • 3
  • 7

Delphi, 231

var g:PByte;a,i,d,n:Int32;begin g:=AllocMem(306);a:=153;Read(i);for n:=1to i do begin g[a]:=2-g[a];d:=d-1+g[a];a:=a+(1-d and 2)*(1+17*(d and 1))end;g[a]:=3;for i:=0to 305do if i mod 18=0then Write(^J) else Write('_ #@'[1+g[i]])end. var g:PByte; a,i,d,n:Int32; begin g:=AllocMem(306); // g: The grid is initially completely white. (size=18*17=289) // Assume 0=d: Ant start 'd'irection faces right (=0, see below) a:=153; // a: Ant position starts at the center of the grid (=8*18+9) Read(i); for n:=1to i do begin // Flip the color of the square; g[a]:=2-g[a]; // Turn 90° right if at an '_' space, 90° left otherwise; d:=d-1+g[a]; // Move one unit forward; // For this, determine the step size, using the two least significant bits of d. // This gives the following relation : // 00 = 0 = 90° = right = 1 // 01 = 1 = 180° = down = 18 // 10 = 2 = 270° = left = - 1 // 11 = 3 = 0° = up = -18 // (d and 2) gives 0 or 2, translate that to 1 or -1 // (d and 1) gives 0 or 1, translate that to 1 or 18 // Multiply the two to get an offset 1, 18, -1 or -18 : a:=a+(1-d and 2)*(1+17*(d and 1)) end; // Place the ant and print the grid : g[a]:=3; // 0 > '_', 2 > '#', 3='@' for i:=0to 305do if i mod 18=0then // we insert & abuse column 0 for newlines only (saves a begin+end pair) Write(^J) else Write('_ #@'[1+g[i]]) end. 

Input:

450 

Output :

_________________ _________________ ___________##____ ____##______##___ ___#__##___##_#__ __###_#@#__#__#__ __#_#_#_#__#_#___ _____###___#_____ _____#___________ _____#__###______ ___#_#_#__#_#_#__ __#__#_#____###__ __#_##__##___#___ ___##______##____ ____##___________ _________________ _________________ 
PatrickvL
  • 691
  • 3
  • 7