g,a:PByte;i,d,n:Int32;begin g:=AllocMem(306);a:=g+153;Read(i);for n:=1to i do begin a^:=2-a^;d:=d-1+a^;a:=a+(1-d and 2)*(1+17*(d and 1))end;a^:=1;for i:=0to 305do if i mod 18=0then Write(^J)else Write('_@#'[1+g[i]])end.var g,a:PByte;i,d,Word;begin g:=AllocMem(306);a:=g+153;Read(i);for n:=1to i do begin a^:=2-a^;d:=d-1+a^;a:=a+(1-2and d)*(1+17*(1and d))end;a^:=1;for n:=1to 306do if n mod 18=0then WriteLn else Write('_@#'[1+g[n]])end.
var g,a:PByte; i,d,n:Int32; begin g:=AllocMem(306); // g: The grid is initially completely white. (size=18*17=306) // Assume 0=d: Ant start 'd'irection faces right (=0, see below) a:=g+153; // a: Ant position starts at the center of the grid (=8*18+9=8*18+9=153) Read(i); for n:=1to i do begin // Flip the color of the square; a^:=2-a^; // Turn 90° right if at an '_' space, 90° left otherwise; d:=d-1+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 and2and 2d)*(1+17*(d and1and 1d)) end; // Place the ant and print the grid : a^:=1; // 0 > '_', 1='@', 2 > '#' for i:=1to 306do if i mod 18=0then // we insert & abuse column 0 for newlines only (saves a begin+end pair) Write(^J)WriteLn else Write('_@#'[1+g[i]]) end.