Codegolf/RGB2Rv10: Difference between revisions
No edit summary |
No edit summary |
||
(16 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
= Esocalc = | |||
Was passiert, wenn man [http://en.wikipedia.org/wiki/Befunge Befunge] | Was passiert, wenn man [http://en.wikipedia.org/wiki/Befunge Befunge] | ||
Line 8: | Line 8: | ||
vereinfachen. | vereinfachen. | ||
== Programmspeicher == | |||
Der Programmspeicher hat 80 * 25 bytes. | Der Programmspeicher hat 80 * 25 bytes. | ||
Line 20: | Line 20: | ||
Der Zeiger wird nicht über die Grenzen der Zeilen/Zellen wandern. | Der Zeiger wird nicht über die Grenzen der Zeilen/Zellen wandern. | ||
== Operatoren == | |||
<code> | <code> | ||
<pre> | <pre> | ||
Line 47: | Line 47: | ||
>3+.^ | >3+.^ | ||
</pre></code> | </pre></code> | ||
Ausgabe ist "5" | Ausgabe ist "5\n" | ||
== Testcases == | |||
git clone https://github.com/maikf/codegolf-rgb2rv10 | |||
Ausführen mit 'GOLF_BIN="yadda yadda" prove -l' | |||
== Lösungen == | |||
=== Perl === | |||
sECuRE (191 bytes): | |||
<code><pre> | |||
GOLF_BIN="perl golf.pl" | |||
</pre></code> | |||
<code><pre> | |||
@a=<>=~/./g;sub x{/\./?print shift,$/:/\d/?@_=($_,@_):/[\/*+-]/&&push@_,int eval(pop.$_.pop);$c+=(1,80,1,-80,-1,-1,80)[$d=/[><^v]/?(42&ord)%7:/[_|]/?1+/_/+2*!!shift:$d]}&x while$_=$a[$c],!/@/ | |||
</pre></code> | |||
=== C === | |||
Merovius (59+186=245 bytes) [http://blog.merovius.de/2013/10/11/how-to-cgolf.html Ausführliche Erklärung] | |||
<code><pre> | |||
GOLF_BIN="cc -D'c(a,x)=*p+3==*#a?x:' -D'o(a,r)=c(a,(*++s=*s r*(s-1),i))' -o golf golf.c; ./golf" | |||
</pre></code> | |||
<code><pre> | |||
main(i){char q[2001],*p=q,*s;for(gets(p);;)p+=i=c(#,i)c(1,(printf("%d\n",*s++),i))c(C,p)o(.,+)o(0,-)o(-,*)o(2,/)c(A,1)c(?,-1)c(a,-80)c(y,80)c(b,*s++?-1:1)c(,*s++?-80:80)(*--s=*p-48,i);} | |||
</pre></code> | |||
adi64 (349 bytes) | |||
<code><pre>GOLF_BIN="cc -ogolf golf.c; ./golf"</pre></code> | |||
<code><pre> | |||
#define S(O) t--;s[t-1]O s[t]; | |||
#define A(X,Y) if(c==X){a=Y;} | |||
#define K(X) if(c==#X[0]){S(X##=)} | |||
int i[2000],s[9],c,z,t;main(a){while((c=getchar())>0)(i[z++]=c)==10?z+=80-z%80:0;z=0;while((c=i[z])!=64){A(118,80)A(94,-80)A(62,1)A(60,-1)A(95,s[--t]?-1:1)A(124,s[--t]?-80:80)K(+)K(-)K(*)K(/)c==46?printf("%d\n",s[--t]):0;c>47&&c<58?s[t++]=c-48:0;z+=a;}} | |||
</pre></code> | |||
koebi (370 bytes) | |||
<code><pre> | |||
GOLF_BIN="cc golf.c -ogolf; ./golf" | |||
</pre></code> | |||
<code><pre> | |||
#define c(a) p[n]==a? | |||
s[9],i,n;main(a){char f[2000],*p=f;for(gets(p);;n+=a){c(43)s[i-2]+=s[--i],s[i]=0:c(45)s[i-2]-=s[--i],s[i]=0:c(42)s[i-2]*=s[--i],s[i]=0:c(47)s[i-2]/=s[--i],s[i]=0:c('v')a=80:c(62)a=1:c(60)a=-1:c(94)a=-80:c(95)s[i-1]==0?a=1:(a=-1),s[--i]=0:c('|')s[i-1]==0?a=80:(a=-80),s[--i]=0:c(46)printf("%d\n",s[--i]),s[i]=0:c(64)p=0,*p:c(' '):(s[i++]=p[n]-48);}} | |||
</pre></code> | |||
=== C++ === | |||
ink (482 bytes) | |||
<code><pre> | |||
GOLF_BIN="g++ golf.c -ogolf; ./golf" | |||
</pre></code> | |||
<code><pre> | |||
#include <cstdio> | |||
#include <unistd.h> | |||
#include <cstring> | |||
#include <stack> | |||
int main(){char q[2000],*n=q,o,r='>';read(0,q,2000);std::stack<int>s;for(;;){o=*n;int a,b;if(o>47&&o<58)s.push(o-48);if(strchr("+-*/",o)) { a=s.top();s.pop();b=s.top();s.pop();s.push(o=='+'?a+b:o=='*'?a*b:o=='-'?b-a:b/a);} if(o=='.') printf("%d\n",s.top()), s.pop();if(strchr("v^<>",o))r=o;if(o=='_')r=!s.top()?62:60;if(o=='|')r=!s.top()?'v':94;if(o=='@') return 0;n=n+(r=='>')-(r<61)+80*((r>94)-(r=='^'));}} | |||
</pre></code> | |||
=== Python === | |||
nicolas (227 bytes) | |||
<code><pre> | |||
GOLF_BIN="python golf.py" | |||
</pre></code> | |||
<code><pre> | |||
s=[];l,p,q,d,v,z,m=raw_input(),s.pop,0,1,80,-80,-1 | |||
while 1:c=l[q];exec(((4*"t=p();s+=[p()%st]|"+4*"d=%s|"+2*"d=p()and %s or %s|"+"print p()|exit()|0|s+=[int(c)]")%tuple("+-*/1mzvm1zv")).split("|")["+-*/><^v_|.@ ".find(c)]);q+=d | |||
</pre></code> | |||
=== Javascript === | |||
Merovius (31 + 392= 423) (codegolf as a service) | |||
<code><pre> | |||
js golf.js GOLF_BIN='curl -d@- http://localhost:9000' | |||
</pre></code> | |||
<code><pre> | |||
require('http').createServer(function(q,r){q.on('readable',function(){p=q.read(2000) | |||
s=[] | |||
i=0 | |||
j=0 | |||
d=1 | |||
a=["d=80","d=1",,"s[j-2]-=s[--j]","r.write(s[--j]+'\\n')","s[j-2]=~~(s[j-2]/s[--j])","d=s[--j]?-80:80",,"s[j-2]*=s[--j]","s[j-2]+=s[--j]",,"r.end()",,,"d=-80","d=s[--j]?-1:1",,,"d=-1"] | |||
for(;;i+=d){(p[i]<48||p[i]>57)?eval(a[(p[i] ^4)%19]):s[j++]=p[i]-48 | |||
if(p[i]==64)return}})}).listen(9000) | |||
</pre></code> | |||
== Post Mortem == | |||
=== Perl === | |||
mxf (151 bytes, nicht kompatibel mit testsuite (GOLF_BIN shellquoting) | |||
<code><pre> | |||
perl -naF -E'sub _{$_=$F[$p];push@_,/[\/*+-]/?eval join$_,@_:/(\d)/;$p+=$m=($m||1,-1,1,-80,80)[y/_|/13/?$_+!pop:y/<>^v/1-4/?$_:0];/\./&&say$==pop;/@/||&_}_' | |||
</pre></code> |
Latest revision as of 11:41, 15 October 2013
Esocalc
Was passiert, wenn man Befunge mit Brainfuck kreuzt? Brainfunge? Befuck?
Dieser Fungeoid wurde stark kastriert um die Implementation zu vereinfachen.
Programmspeicher
Der Programmspeicher hat 80 * 25 bytes. Daten werden auf einem Stack gespeichert, die meisten Operatoren poppen die zwei letzten Elemente und pushen das Ergebnis an das Ende. (erst "a", dann "b")
Initial ist der Zeiger auf dem ersten Byte und wandert nach rechts.
Man kann davon ausgehen, dass nur valide Programme via stdin übergeben werden. Der Zeiger wird nicht über die Grenzen der Zeilen/Zellen wandern.
Operatoren
0-9 Push this number on the stack
+ Addition: Pop a and b, then push a+b
- Subtraction: Pop a and b, then push b-a
* Multiplication: Pop a and b, then push a*b
/ Integer division: Pop a and b, then push b/a, rounded down.
> Start moving right
< Start moving left
^ Start moving up
v Start moving down
_ Pop a value; move right if value=0, left otherwise
| Pop a value; move down if value=0, up otherwise
. Pop value and output as an integer plus a newline
@ End program
(space) No-op. Does nothing
Beispiel ("pretty printed")
v
2 @
>3+.^
Ausgabe ist "5\n"
Testcases
git clone https://github.com/maikf/codegolf-rgb2rv10
Ausführen mit 'GOLF_BIN="yadda yadda" prove -l'
Lösungen
Perl
sECuRE (191 bytes):
GOLF_BIN="perl golf.pl"
@a=<>=~/./g;sub x{/\./?print shift,$/:/\d/?@_=($_,@_):/[\/*+-]/&&push@_,int eval(pop.$_.pop);$c+=(1,80,1,-80,-1,-1,80)[$d=/[><^v]/?(42&ord)%7:/[_|]/?1+/_/+2*!!shift:$d]}&x while$_=$a[$c],!/@/
C
Merovius (59+186=245 bytes) Ausführliche Erklärung
GOLF_BIN="cc -D'c(a,x)=*p+3==*#a?x:' -D'o(a,r)=c(a,(*++s=*s r*(s-1),i))' -o golf golf.c; ./golf"
main(i){char q[2001],*p=q,*s;for(gets(p);;)p+=i=c(#,i)c(1,(printf("%d\n",*s++),i))c(C,p)o(.,+)o(0,-)o(-,*)o(2,/)c(A,1)c(?,-1)c(a,-80)c(y,80)c(b,*s++?-1:1)c(?,*s++?-80:80)(*--s=*p-48,i);}
adi64 (349 bytes)
GOLF_BIN="cc -ogolf golf.c; ./golf"
#define S(O) t--;s[t-1]O s[t];
#define A(X,Y) if(c==X){a=Y;}
#define K(X) if(c==#X[0]){S(X##=)}
int i[2000],s[9],c,z,t;main(a){while((c=getchar())>0)(i[z++]=c)==10?z+=80-z%80:0;z=0;while((c=i[z])!=64){A(118,80)A(94,-80)A(62,1)A(60,-1)A(95,s[--t]?-1:1)A(124,s[--t]?-80:80)K(+)K(-)K(*)K(/)c==46?printf("%d\n",s[--t]):0;c>47&&c<58?s[t++]=c-48:0;z+=a;}}
koebi (370 bytes)
GOLF_BIN="cc golf.c -ogolf; ./golf"
#define c(a) p[n]==a?
s[9],i,n;main(a){char f[2000],*p=f;for(gets(p);;n+=a){c(43)s[i-2]+=s[--i],s[i]=0:c(45)s[i-2]-=s[--i],s[i]=0:c(42)s[i-2]*=s[--i],s[i]=0:c(47)s[i-2]/=s[--i],s[i]=0:c('v')a=80:c(62)a=1:c(60)a=-1:c(94)a=-80:c(95)s[i-1]==0?a=1:(a=-1),s[--i]=0:c('|')s[i-1]==0?a=80:(a=-80),s[--i]=0:c(46)printf("%d\n",s[--i]),s[i]=0:c(64)p=0,*p:c(' '):(s[i++]=p[n]-48);}}
C++
ink (482 bytes)
GOLF_BIN="g++ golf.c -ogolf; ./golf"
#include <cstdio>
#include <unistd.h>
#include <cstring>
#include <stack>
int main(){char q[2000],*n=q,o,r='>';read(0,q,2000);std::stack<int>s;for(;;){o=*n;int a,b;if(o>47&&o<58)s.push(o-48);if(strchr("+-*/",o)) { a=s.top();s.pop();b=s.top();s.pop();s.push(o=='+'?a+b:o=='*'?a*b:o=='-'?b-a:b/a);} if(o=='.') printf("%d\n",s.top()), s.pop();if(strchr("v^<>",o))r=o;if(o=='_')r=!s.top()?62:60;if(o=='|')r=!s.top()?'v':94;if(o=='@') return 0;n=n+(r=='>')-(r<61)+80*((r>94)-(r=='^'));}}
Python
nicolas (227 bytes)
GOLF_BIN="python golf.py"
s=[];l,p,q,d,v,z,m=raw_input(),s.pop,0,1,80,-80,-1
while 1:c=l[q];exec(((4*"t=p();s+=[p()%st]|"+4*"d=%s|"+2*"d=p()and %s or %s|"+"print p()|exit()|0|s+=[int(c)]")%tuple("+-*/1mzvm1zv")).split("|")["+-*/><^v_|.@ ".find(c)]);q+=d
Javascript
Merovius (31 + 392= 423) (codegolf as a service)
js golf.js GOLF_BIN='curl -d@- http://localhost:9000'
require('http').createServer(function(q,r){q.on('readable',function(){p=q.read(2000)
s=[]
i=0
j=0
d=1
a=["d=80","d=1",,"s[j-2]-=s[--j]","r.write(s[--j]+'\\n')","s[j-2]=~~(s[j-2]/s[--j])","d=s[--j]?-80:80",,"s[j-2]*=s[--j]","s[j-2]+=s[--j]",,"r.end()",,,"d=-80","d=s[--j]?-1:1",,,"d=-1"]
for(;;i+=d){(p[i]<48||p[i]>57)?eval(a[(p[i] ^4)%19]):s[j++]=p[i]-48
if(p[i]==64)return}})}).listen(9000)
Post Mortem
Perl
mxf (151 bytes, nicht kompatibel mit testsuite (GOLF_BIN shellquoting)
perl -naF -E'sub _{$_=$F[$p];push@_,/[\/*+-]/?eval join$_,@_:/(\d)/;$p+=$m=($m||1,-1,1,-80,80)[y/_|/13/?$_+!pop:y/<>^v/1-4/?$_:0];/\./&&say$==pop;/@/||&_}_'