Perl, 110 characters
$p=144;$p+=(1,-17,-1,17)[($d+=($f[$p]^=2)+1)%4]for 1..<>;$f[$p]=1;print$_%17?'':$/,qw(_ @ #)[$f[$_]]for 0..288 Number is read from the first line of STDIN. Rest of input is ignored.
Slightly more readable:
$p=144; $p += (1,-17,-1,17)[($d+=($f[$p]^=2)+1) % 4] for 1..<>; $f[$p]=1; print $_%17 ? '' : $/, qw(_ @ #)[$f[$_]] for 0..288 Edits
(112 → 111) No need to update
$dwith the modulo-4 value.(111 → 110) Can now inline the
$dincrement
Addendum (109 characters)
We can have it one character shorter if you’re happy to have the special case of N=0 fail (it doesn’t output the @ character for the ant). All other inputs work correctly:
$p+=(1,-17,-1,17)[($d+=($f{$p+0}^=2)+1)%4]for 1..<>;$f{$p}=1;print$_%17-9?'':$/,qw(_ @ #)[$f{$_}]for-144..144 The differences are that we now use %f instead of @f so we can use negative indices, and we iterate from -144..144 instead of 0..288. It saves having to initialise $p.