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; }
int exec_cmd(int under_glob, int under_until) { register int status; register char *p; int n; if((status = getrange()) <= ERROR) return( status ); status = ERROR9; switch( *lp++ ) { case 'i': laddr2 = prevln(laddr2); case 'a': status = append(laddr2, under_glob); break; case 'b': if(!under_glob && !under_until) status = branch(); break; case 'c': if((status = delete(laddr1, laddr2, SAVE)) == OK) status = append(prevln(laddr1), under_glob); break; case 'd': if((status = delete(laddr1, laddr2, SAVE)) == OK && nextln(curln) != 0) curln = nextln(curln); break; case 'e': if(lastln && dirty && *lp != 'e') { status = ERROR4; break; } if(*lp == 'e') ++lp; if(nladdrs == 0 && !under_glob && !under_until && (status = getfn()) == OK) { set_fn(curfile, lp); if(lastln != 0) delete(1, lastln, NOSAVE); num_delete_lines = 0; if((status = _read( lp, 0, 0)) == OK) { dirty = 0; if(lastln) curln = 1; } } lp = "\n"; break; case 'f': if(nladdrs == 0 && (status = getfn()) == OK) { set_fn(curfile, lp); putmsg(curfile); lp = "\n"; } change_state(CMD); break; case 'g': if(!under_glob) { if(*lp == '^') { ++lp; n = 0; } else n = 1; status = exec_glob(n, under_until); } break; case 'h': n = getint(); #ifndef __STDC__ while(n--) for(n1 = 0; n1 < 10; ++n1) time_slice(); #endif status = OK; break; case 'j': status = join(laddr2); break; case 'k': if((status = get_laddr_expr(&n)) == OK) status = kopy(n); break; case 'l': if(nladdrs == 0) status = learn(); break; case 'm': if((status = get_laddr_expr(&n)) == OK) status = move(n); break; case 'o': status = option(); break; case 'p': case 'P': status = prnt(laddr1, laddr2); break; case 'q': if(nladdrs==0 && !under_glob) { if((*lp=='\n' && !dirty) || *lp=='q' || lastln == 0) status = EOF; else status = ERROR4; } break; case 'r': if(!under_glob && !under_until && (status = getfn()) == OK) status = _read(lp, laddr2, 0); lp = "\n"; break; case 's': n = getint(); /* read occurance if present */ if((status=getpat()) == OK && (status=getsubst_str(tbuff)) == OK) status = substitute(tbuff, under_glob, n); break; case 't': case 'T': if(nladdrs == 0) status = translate(*(lp - 1) == 't'); break; case 'u': status = until(laddr1, laddr2, under_glob); break; case 'v': if(nladdrs <= 1) status = view(laddr1); break; case 'w': n = 0; if(*lp == 'w') { n |= 2; ++lp; } if(*lp == 'a') { n |= 1; ++lp; } if((status = getfn()) == OK) { if(nladdrs == 0) { if(curfile[0] == '\0') set_fn(curfile, lp); else { if((n & 2) == 0 && strcmp(curfile, lp) != 0) { for(p = lp ; *p ; ++p) if(*p == '$') goto ok; puterr(-19); lp = "\n"; break; } } ok: if((status = _write(lp, 1, lastln, n & 1, 0)) == OK) dirty = 0; } } else { status = _write(lp, laddr1, laddr2, n & 1, 0); } lp = "\n"; break; case 'x': if(!under_glob && !under_until && (status = getfn()) == OK) if(nladdrs == 0) status = exec_file(lp, under_glob, under_until); lp = "\n"; break; case 'y': status = yut(); break; case 'z': status = zap(); break; case '\n': --lp; /* put back newline for main */ if(laddr2 < 0 || laddr2 > lastln) status = lastln ? ERROR5 : OK; else { curln = laddr2; status = OK; } break; case ' ': case '\t': case CMD_CHAR: status = OK; break; case '=': dtoc(rbuff, laddr2); if(under_glob) prnt_screen(rbuff, 1); else putmsg(rbuff); status = OK; break; case '"': lp = "\n"; /* Ignore rest of line */ status = OK; break; case '!': if(escape_char == 0 || restrict_flag) { putmsg("Escape from ED inhibited"); status = NOTHING; } else status = exec_sys_cmd(under_glob, under_until); break; default: status = ERROR2; } return(status); }
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); } } }