Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Created November 24, 2019 09:38
Show Gist options
  • Select an option

  • Save hastebrot/68a0484db9ca54deaca1182e5ecd4a78 to your computer and use it in GitHub Desktop.

Select an option

Save hastebrot/68a0484db9ca54deaca1182e5ecd4a78 to your computer and use it in GitHub Desktop.
Solver for the c't puzzle by Uli Schuhmacher.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define C if
#define T void
#define P NULL
#define U while
#define Z break
#define S return
#define L typedef
#define E clock()
#define _ (double)
#define W continue
#define X(a,b) for(a=0;a<b;a++)
L int I;L long long B;I R[]={203,158,1625,151,214,91,4123,186,36867,587,94,601}
,K,g[9],M[4][4];B*d[60],*k[12],e[12][1440],f;I A(I x,I y,I z){C(x<3&&y<4&&z<5&&
x>=0&&y>=0&&z>=0)S x+y*3+z*3*4;S-1;}B Y(I*v){I b;b=A(v[0],v[1],v[2]);S b!=-1?(B
)1<<b:0;}T G(I*v,I x,I y,I z,I u){v[0]=x;v[1]=y;v[2]=z;v[3]=u;}T H(I*v,I*w){I i
;X(i,4)v[i]=w[i];}B J(B p,I a,I b,I c){I x,y,z,v[4];B u;u=0;X(x,3)X(y,4)X(z,5){
G(v,x,y,z,1);C(p&Y(v)){G(v,a?2-x:x,b?3-y:y,c?4-z:z,1);u|=Y(v);}}S u;}T V(I N){I
x,y,z,i,j,v[4],w[4];B p,s=0;X(x,3)X(y,4)X(z,2){C(!(1<<(x+y*3+z*12)&R[N]))W;G(v,
x,y,z,1);X(i,4){w[i]=0;X(j,4)w[i]+=v[j]*M[j][i];}p=Y(w);C(!p)S;s|=p;}i=0;U(p=e[
N][i]){C(p==s)Z;C(N==6&&(p==J(s,1,1,0)||p==J(s,1,0,1)||p==J(s,0,1,1))){C((s&-s)
>(p&-p))e[N][i]=s;Z;}i++;}C(!p){e[N][i++]=s;e[N][i]=0;}}T Q(B F,I w,B mi){B*m;I
N,n;U(F&mi){mi<<=1;w++;}C(w==60){C(!(++K%1000)){putchar('.');fflush(stdout);}S;
}m=d[w];U(*m){N=*m>>16;n=*m++&8191;C(k[N]){m+=n;W;}U(n--){C(!(*m&F)){k[N]=m;f++
;Q(F|*m,w,mi);}m++;}k[N]=P;}}T O(T){I i,w,N,l,n,o,q,r;B*t,*h,p;g[2]=-1;g[5]=1;X
(N,12){X(l,6){H(M[0],&g[5-l]);X(n,6){H(M[1],&g[5-n]);C(l%3==n%3)W;X(o,3){q=(o+1
)%3;r=(o+2)%3;M[2][o]=M[0][q]*M[1][r]-M[0][r]*M[1][q];}X(o,3)X(q,4)X(r,5){G(M[3
],o,q,r,1);V(N);}}}}X(w,60){t=d[w]=(B*)malloc(1500*sizeof(B));X(N,12){h=P;i=0;U
(p=e[N][i++]){C(!(((B)1<<w)&p))W;C((((B)1<<w)-1)&p)W;C(!h)h=t++;*t++=p;}C(h)*h=
(N<<16)|(t-h-1);}*t++=0;}Q(0,0,1);}int main(T){clock_t s=E;O();
printf("\n%d Loesungen, %.2fs, %.0f Tests\n",K,_(E-s)/CLOCKS_PER_SEC,_ f);S 0;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment