int readPatList(LifeList *pat, char *patname) { int i=0; FILE *patfile; char header[256]; int patcount=0; header[0] = '\0'; if ( (patfile=fopen(patname, "r")) != NULL) { while (fgets(s, 8191, patfile)) { if (iswhitespace(s)) continue; if (s[0] == '#') { if (s[1] == '#') { if (patcount && header[0]) { //fprintf(stderr, "%s x %d\n", header, patcount); patcount = 0; } strcpy(header, s); header[strcspn(header, "\r\n")] = '\0'; } } else { initLifeList(pat+i); getpat(s, pat+i); i++; patcount++; } } //if (patcount && header[0]) // fprintf(stderr, "%s x %d\n", header, patcount); //fprintf(stderr, "Total catalysts %d\n", i); fclose(patfile); } return i; }
main(int argc, char *argv[]) { static LifeList cells; static LifeList matchcells; static LifeList outcells; int i,j,n=0; int gens=500; int x,y; int match; int pos; int count[240]; int least= 0; int fail; int damaged; int restored; int prodcells; int restorefilter=10; char outpat[100000]; History hist; int period, repetitions; Cell lifetrail[5000]; static char nextpat[10000]; int ncls; initLifeList(&cells); initLifeList(&matchcells); initLifeList(&outcells); if (argc>2) { sscanf(argv[2], "%d", &restorefilter); } while(gets(nextpat)) { for (i=0; i<240; i++) { count[i] = i; } getpat(nextpat, &cells); copyLifeList(&cells, &matchcells); if (argc>1) { for (i=0; argv[1][i]; i++) { matchcells.ncells = removeIfEquals(matchcells.cellList, matchcells.ncells, argv[1][i]); } } fail=0; damaged=0; restored=0; for (i=0; i<gens; i++) { generate(&cells); // printf("%d %d %d %d\n", i, fail, damaged, restored); count[i%240] = cells.ncells; if (cells.ncells == 0) break; match = 1; for (j=0; j<180; j++) { if (count[j] != count[j+60]) { match = 0; break; } } if (match) break; if ( matchLifeList(&cells, &matchcells, 0) < matchcells.ncells) { fail++; restored=0; } else { prodcells = cells.ncells - matchcells.ncells; copyLifeList(&cells, &outcells); restored++; if (restored>restorefilter) { if (fail>damaged) damaged=fail; fail = 0; } } if (fail>150) break; } if (damaged) { fflush(stdout); removeLifeList(&outcells, &matchcells, 0); makeString(outcells.cellList, outcells.ncells, outpat); printf("%s %d %d %d %d %s\n", nextpat, damaged, fail, i-fail, prodcells, outpat); } } }
main(int argc, char *argv[]) { static LifeList cells; static LifeList matchcells; static LifeList boundarycells; static LifeList intercells; static LifeList outcells; int i,j,n=0; int gens=500; int x,y; int match; int pos; int count[240]; int least= 0; int fail; int damaged; int restored; int firstgen; int maxgen; int survivegen; int restorefilter=10; int maxrecoverytime=20; char interpat[100000]; char outpat[100000]; History hist; int period, repetitions; Cell lifetrail[50000]; static char nextpat[100000]; int ncls; initLifeList(&cells); initLifeList(&matchcells); initLifeList(&boundarycells); initLifeList(&intercells); initLifeList(&outcells); if (argc>2) { sscanf(argv[2], "%d", &restorefilter); } while(gets(nextpat)) { for (i=0; i<240; i++) { count[i] = i; } getpat(nextpat, &cells); copyLifeList(&cells, &matchcells); if (argc>1) { for (i=0; argv[1][i]; i++) { matchcells.ncells = removeIfEquals(matchcells.cellList, matchcells.ncells, argv[1][i]); } } copyLifeList(&matchcells, &boundarycells); spread(&boundarycells, 1); fail=0; damaged=0; restored=0; firstgen=0; maxgen=0; survivegen=0; for (i=0; i<gens; i++) { // printf("%d %d %d %d\n", i, fail, damaged, restored); count[i%240] = cells.ncells; if (cells.ncells == 0) break; match = 1; for (j=0; j<180; j++) { if (count[j] != count[j+60]) { match = 0; break; } } if (match) break; if ( matchLifeList(&cells, &matchcells, 0) < matchcells.ncells || matchLifeList(&cells, &boundarycells, 0) != matchcells.ncells) { fail++; restored=0; if (firstgen == 0) { firstgen = i; maxgen = i + restorefilter + maxrecoverytime; } } else { restored++; survivegen = i; if (restored>restorefilter) { if (restored == restorefilter + 1) { copyLifeList(&cells, &intercells); copyLifeList(&cells, &outcells); } if (i <= maxgen) { copyLifeList(&cells, &outcells); } if (fail>damaged) damaged=fail; fail = 0; firstgen = 0; } } if (fail>150) break; generate(&cells); } if (damaged) { fflush(stdout); removeLifeList(&outcells, &matchcells, 0); makeString(outcells.cellList, outcells.ncells, outpat); removeLifeList(&intercells, &matchcells, 0); makeString(intercells.cellList, intercells.ncells, interpat); printf("%s %d %s %s\n", nextpat, survivegen, interpat, outpat); } } }