Codegolf/RGB2Rv10: Difference between revisions

From NoName e.V.
Jump to navigation Jump to search
Line 105: Line 105:


=== Post Mortem ===
=== Post Mortem ===
== Perl ==
mxf (155 bytes, nicht kompatibel mit testsuite)
<code><pre>
perl -naF -E'sub _{$_=$F[$p];@_=(/[\/*+-]/?eval pop.$_.pop:/(\d)/,@_);/\./&&say$==shift;$p+=$m=($m||1,-1,1,-80,80)[y/_|/13/?$_+!pop:y/<>^v0-9/1-4/dr];/@/||&_}_'
</pre></code>

Revision as of 16:33, 10 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)

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);}

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

Post Mortem

Perl

mxf (155 bytes, nicht kompatibel mit testsuite)

perl -naF -E'sub _{$_=$F[$p];@_=(/[\/*+-]/?eval pop.$_.pop:/(\d)/,@_);/\./&&say$==shift;$p+=$m=($m||1,-1,1,-80,80)[y/_|/13/?$_+!pop:y/<>^v0-9/1-4/dr];/@/||&_}_'