So I thought I'd implement Langtons ant without if, switch or the like:
#include <stdio.h>
#define W 8 //*32
#define H 256
#define CYCLES 12300
int main(){
short int x=W*16-1,y=H/2-1,d=2,c,dy,ax,bx,i;
int field[W][H];
for(i=0;i<CYCLES;i++){
x+=d/2+d%2+d%3%2-2;
dy=d%4+1;
y+=dy/2+dy%2+dy%3%2-2;
ax=x/32,bx=x%32;
c=field[ax][y]>>bx&1;
d=(d+2*c)%4+1;
field[ax][y]^=1<<bx;
printf("i: %d\tx: %d\ty: %d\td: %d\n",i,x,y,d);
}
}
while the transitions for x and y (and d) are correct, they are still somewhat complicated... an ideas how to simplify/optimize them?
Does it bite?
#define DD(d) d/2+d%2+d%3%2-2
dy=d&3;d++;x+=DD(d);y+=DD(dy);
ax=x>>5;bx=(x&31)-1;c=field[ax][ay]>>b&1;
d+=c;d&=3;d++;field[ax][y]^=2<<bx;