dir char anyfile(0) [ pl "" char where(10000),lfs(1);int cnt,ix,lines lfs(0)=10 if ((cnt=readfile(anyfile,where,where+10000,1))>0) [ while ((ix=index(where,cnt,lfs,1))>0) [ pft where,where+ix-1 where=where+ix cnt=cnt-ix if (++lines>19) [sak;pl "";pl "";lines=0] ]]] setup [ hilo '1';color '3' posc 10,20;ps "mykarel.tc - tct - 12/19/16 - p1" color '7' posc 21,20;ps "seed (odd number from 1-999)? ";last=seed=gn posc 22,20;ps "bag (0-9999)? ";bag=gn off ] /* ref: http://primepuzzle.com/tc/karel-problem-book.html */ turnaround [turnleft;turnleft] fetch_beeper [ while (not_next_to_a_beeper) move pickbeeper ] goto_wall [ while (front_is_clear) move ] turnright [turnleft;turnleft;turnleft] main [ setup putbeeper /* leave marker */ turnright move turnright for (i=1;i<=2;++i) fetch_beeper turnright for (i=1;i<=4;++i) move turnright fetch_beeper turnright goto_wall turnright while (left_is_blocked) move turnleft;move;turnleft goto_wall;turnright move turnleft move turnleft goto_wall;putbeeper;putbeeper;putbeeper turnaround;move;turnleft;move;turnleft goto_wall;turnright fetch_beeper ] int cnt,i,j,k,l,p,bag,ka,ks,q char iwa(24*80),kc,wall(400) char gworld(12) reverse char s(0) [ int c,i,j j=strlen(s)-1 while (i=wt;--wwb) iwa(wwb+24*wa)='*' ] strcat wall,wwrk;strcat wall,crlf ] if (ceqn(rl(0),"b",1)) [ atoi rl(1),rla;atoi rl(2),rls;atoi rl(3),rlbeep ba=4*rla(0) bs=21-2*rls(0) if (rlbeep(0)==1) bc='@' else bc=rlbeep(0)+'0' if (rlbeep(0)==0) bc='!' iwa(24*ba+bs)=bc ] if (ceqn(rl(0),"g",1)) [ atoi rl(1),rla bag=rla(0) ] ] readworld char iw(0) [ int i,j,r,c,s,br,wl(200);char w(2600),lf(1),lits(100) for (r=0;r<24;++r) [ for (c=0;c<80;++c) iwa(r+24*c)=' ' ] lf(0)=10 br=readfile iw,w,w+2600-1,1 while (i='2')*(iwa(24*a+s)<='9') beeps=beeps+(iwa(24*a+s)=='!') strcpy mcs," " if (beeps) [ if (iwa(24*a+s)=='@') strcat mcs,"1" else if (iwa(24*a+s)=='!') strcat mcs,"0" else mcs(1)=iwa(24*a+s) n(0)=a/4 itoa n(0),st strcat w,"b " strcat w,st strcat w," " n(0)=(22-s)/2 itoa n(0),st strcat w,st strcat w,mcs strcat w,crlf ]]] strcat w,"k " n(0)=ka/4 itoa n(0),st strcat w,st strcat w," " n(0)=(22-ks)/2 itoa n(0),st strcat w,st if (kc=='^') strcat w," N" else if (kc=='v') strcat w," S" else if (kc=='>') strcat w," E" else strcat w," W" strcat w,crlf strcat w,"g " n(0)=bag itoa n(0),st strcat w,st strcat w,crlf strcat w,wall strcpy ow,"n" strcat ow,iw writefile ow,w,w+strlen(w)-1,1 pl w ] right_is_blocked [ int ta if (kc=='^') ta=24*(ka+2)+ks else if (kc=='<') ta=24*ka+ks-1 else if (kc=='v') [ if (ka-2==0) return 1 else ta=24*(ka-2)+ks ] else if (ks+1==20) return 1 else ta=24*ka+ks+1 return iwa(ta)=='*' ] right_is_clear [return !right_is_blocked] left [ char ks(4),kcw(1) strcpy ks,"v>^='2')*(iwa(24*ka+ks)<='9') return t ] not_next_to_a_beeper [return !next_to_a_beeper] facing_n [return kc=='^'] not_facing_n [return !facing_n] facing_s [return kc=='v'] not_facing_s [return !facing_s] facing_e [return kc=='>'] not_facing_e [return !facing_e] facing_w [return kc=='<'] not_facing_w [return !facing_w] any_beepers_in_beeper_bag [return bag] no_beepers_in_beeper_bag [return !any_beepers_in_beeper_bag] move [ if (front_is_blocked) turnoff(1) if (kc=='^') ks=ks-2 else if (kc=='v') ks=ks+2 else if (kc=='>') ka=ka+4 else ka=ka-4 if ((ka/4<1)+((22-ks)/2<1)) turnoff(3) else if ((ka/4>18)+((22-ks)/2>10)) turnoff(5) if q!=1 printworld wait p ] turnleft [ left if q!=1 printworld wait p ] pickbeeper [ char wbc,bc if (not_next_to_a_beeper) turnoff(2) wbc=iwa(24*ka+ks) if (wbc=='@') bc=' ' else if (wbc=='2') bc='@' else if (wbc=='!') bc='9' else bc=wbc-1 iwa(24*ka+ks)=bc ++bag if q!=1 printworld wait p ] putbeeper [ char wbc,bc if (no_beepers_in_beeper_bag) turnoff(4) wbc=iwa(24*ka+ks) if ((wbc=='9')+(wbc=='!')) bc='!' else if (wbc==' ') bc='@' else if (wbc=='@') bc='2' else bc=wbc+1 iwa(24*ka+ks)=bc --bag if q!=1 printworld wait p ] turnoff int err [ int msg(10);char foo(150) printworld wait 1 strcpy foo,"Mission complete!,Front is blocked,Not next to beeper," strcat foo,"Boundary detected,No beepers in bag,Went out of bounds," strcat foo,"Too many beepers," split msg,foo,"," posc 24,33;beep(440,2);ps msg(err) saveworld gworld if (err) [pl "";on;exit] ] rwait int p [ int i,j for (i=0;i