Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created December 28, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/4153b258494bca89e3f8d32544df420a to your computer and use it in GitHub Desktop.
Save Hermann-SW/4153b258494bca89e3f8d32544df420a to your computer and use it in GitHub Desktop.
Presenter of peg-solitaire solution from 2⁽n-3) length solution file
/* gcc -O6 -Wall -pedantic -Wextra sol2.c -Wno-long-long -o sol2 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
#include <unistd.h>
char B[9][9]={
#if 0
".........", /* English */
"...ooo...",
"...ooo...",
".ooooooo.",
".oooxooo.",
".ooooooo.",
"...ooo...",
"...ooo...",
"........."
#elif 0
".........", /* French */
"...ooo...",
"..ooooo..",
".oooxooo.",
".ooooooo.",
".ooooooo.",
"..ooooo..",
"...ooo...",
"........."
#else
"...ooo...", /* 3-3-2-2 */
"...ooo...",
"...ooo...",
".oooooooo",
".oooxoooo",
".oooooooo",
"...ooo...",
"...ooo...",
"........."
#endif
};
int N[9][9];
int m,cnt;
uint64_t M[7*9*2*2];
uint64_t D[7*9*2*2];
uint64_t P[7*9*2*2];
void draw(uint64_t u, int p)
{
int i,j;
printf("\x1b[2J");
for(i=0; i<9; ++i)
{
for(j=0; j<9; ++j)
printf("%s", N[i][j]==255 ? "." : (u & (1LL<<N[i][j])) ?
((N[i][j]==p) ? "\x1b[7mo\x1b[0m" : "o") : " ");
printf("\n");
}
usleep(p==255 ? 900000 : 600000);
}
uint8_t fgtc(FILE *src, uint64_t u)
{
int c;
rewind(src);
assert(0 == fseek(src, u, SEEK_SET));
c = fgetc(src);
assert(c != EOF);
return c;
}
int main(int argc, char *argv[])
{
uint64_t u;
FILE *src;
int i,d;
uint64_t j,n;
assert(argc==2);
sscanf(argv[1],"%"SCNx64,&u);
m=0;
for(i=0; i<9; ++i)
{
for(j=0; j<9; ++j)
{
N[i][j] = (B[i][j]!='.') ? cnt++ : 255;
if (i>1)
if (N[i][j]!=255 && N[i-1][j]!=255 && N[i-2][j]!=255)
{
M[m] = (1LL<<N[i-2][j]);
P[m] = N[i][j];
D[m++] = (1LL<<N[i][j]) | (1LL<<N[i-1][j]);
M[m] = (1LL<<N[i][j]);
P[m] = N[i-2][j];
D[m++] = (1LL<<N[i-1][j]) | (1LL<<N[i-2][j]);
}
if (j>1)
if (N[i][j]!=255 && N[i][j-1]!=255 && N[i][j-2]!=255)
{
M[m] = (1LL<<N[i][j-2]);
P[m] = N[i][j];
D[m++] = (1LL<<N[i][j]) | (1LL<<N[i][j-1]);
M[m] = (1LL<<N[i][j]);
P[m] = N[i][j-2];
D[m++] = (1LL<<N[i][j-1]) | (1LL<<N[i][j-2]);
}
}
}
assert( (src=fopen("3-3-2-2","rb")) );
i = fgtc(src, u>>3);
assert(i & 1<<(u%8));
for(;;)
{
/* printf("%"PRIx64"\n",u); */
draw(u, 255);
n=0;
for(d=0; d<m; ++d)
{
if (!(u & M[d]) && ((u & D[d]) == D[d]))
{
n = u^(M[d]|D[d]);
i = fgtc(src, n>>3);
if (i & 1<<(n%8))
break;
}
}
if (n==0)
break;
draw(u, P[d]);
u=n;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment