Codegolf/3: Difference between revisions

From NoName e.V.
Jump to navigation Jump to search
m (Immernoch kaputt, aber zumindest stimmt mal der Score ;))
 
(16 intermediate revisions by 7 users not shown)
Line 37: Line 37:
  assert_eq `#{ARGV[0]} 1. März 2000`, "Mittwoch\n"
  assert_eq `#{ARGV[0]} 1. März 2000`, "Mittwoch\n"
  assert_eq `#{ARGV[0]} 1. Januar 1712`, "Freitag\n"
  assert_eq `#{ARGV[0]} 1. Januar 1712`, "Freitag\n"
assert_eq `#{ARGV[0]} 1. Februar 2000`, "Dienstag\n"


(Score: 498)
(Score: 553)


  ./test.rb ./wochentag
  ./test.rb ./wochentag
Line 56: Line 57:
  t "1. März 2000" "Mittwoch"
  t "1. März 2000" "Mittwoch"
  t "1. Januar 1712" "Freitag"
  t "1. Januar 1712" "Freitag"
t "1. Februar 2000" "Dienstag"


(Score: 331)
(Score: 364)


  ./test.sh ./wochentag
  ./test.sh ./wochentag
Line 63: Line 65:
=== Inoffizielle, vom Schiri nicht bestätigte Scores ===
=== Inoffizielle, vom Schiri nicht bestätigte Scores ===


=== Shellscripting===
==== Shellscripting ====
* sECuRE / zsh: 147
* sECuRE / zsh (neu): 132
* jiska / bash: 246
* sECuRE / zsh (alt): 148
* jiska / bash: 227
* cato / bash: 274
* cato / bash: 274
* [[User:CentronX|CentronX]] / PowerShell: ca. 800 (Ja, I know!)
* [[User:CentronX|CentronX]] / PowerShell: ca. 800 (Ja, I know!)


=== Perl ===
==== Perl ====
* mxf: 164
* mxf: 152
* urs: 171
* urs: 171
* yath: 188
* yath: 181
* downhill: 245
* downhill: 245


=== Python ===
==== Python ====
* Nicolas / Python: 190
* Nicolas / Python: 190
* [[User:Kungi|Kungi]] / Python: 257
* [[User:Kungi|Kungi]] / Python: 257


=== Ruby ===
==== Ruby ====
* [[User:PhilFry|PhilFry]] / Ruby: 215
* [[User:PhilFry|PhilFry]] / Ruby: 181


=== C ===
==== C ====
* sECuRE / C: 260 byte (normal), 220 byte (obfuscated, ohne compileraufruf), 132 byte (obfuscated, nicht rules-compliant, ohne compileraufruf)
* sECuRE / C: 261 byte (normal), 221 byte (obfuscated, ohne compileraufruf), 133 byte (obfuscated, nicht rules-compliant, ohne compileraufruf)


=== ABAP ===
==== ABAP ====
* SdK / ABAP: 1586
* SdK / ABAP: 1586


=== PHP ===
==== PHP ====
* jiska / PHP: 295
* BugBlue / PHP: 345
* BugBlue / PHP: 345
=== Offizielle Ergebnisse ===
Zunächst mal das Ergebnis der Validierung, Quellcodes kommen noch
C/sECuRE/wochentag-6: ok
C/sECuRE/obfuscated-6: ok
bash/cato/golf3.sh: nicht utf8-clean, bash/cato/golf3.sh: line 5: 09: value too great for base (error token is "09")
bash/jiska/wochentag.sh: ok
Perl/apic/wochentag: ok
Perl/downhill/golf2.pl: ok
Perl/mxf/mxf-wochentag-5a.pl: Failed for 25. Oktober 1746, returned Sonntag instead of Dienstag
Perl/urs/urs.pl: Failed for 21. Februar 1828, returned Mittwoch instead of Donnerstag
Perl/yath/foo.pl: Failed for 5. Dezember 1949, returned Dienstag instead of Montag
PHP/jiska/jiska-wochentag.php: ok
PowerShell/CentronX/pastebin.cmd: wie testen? woher kommt $d? Fehlt da was?
Python/kungi/wochentag.py: nicht utf8-clean, Failed for 12. Januar 1900, returned Donnerstag instead of Freitag
Python/nic0las/wd.py: ok
Ruby/phil_fry/wochentag.rb: ok
Ruby/phil_fry/wochentag-phil.rb: Failed for 5. Dezember 1949, returned Sonntag instead of Montag
zsh/sECuRE/wochentag-3.sh: ok
zsh/sECuRE/wochentag-5.sh: ok
==== bash ====
===== cato =====
a=(Samstag Sonntag Montag Dienstag Mittwoch Donnerstag Freitag)
case ${2:1:2} in an)m=14;;eb)m=15;;är)m=4;;pr)m=5;;ai)m=6;;un)m=7;;ul)m=8;;ug)m=9;;ep)m=10;;kt)m=11;;ov)m=12;;ez)m=13;; esac
y=$[$3-(m>13)];k=${y:2};j=${y:0:2}
echo ${a[$[(${1/.}+($m*26)/10+k+k/4+j/4+5*j)%7]]}
Score: 275
===== jiska =====
t=(J F z A Ma ni li Au S O N D)
w=(SonnX MonX DiensX Mittwoch DonnersX FreiX SamsX)
for((i=0;i<12;i++));do echo $2|grep -q ${t[i]}&&m=$[i+2];done
y=$3
echo ${w[$[m<4&&(m+=12,y--),(${1/./}+13*m/5+y+y/4-y/100+y/400+6)%7]]/X/tag}
Score: 227
==== C ====
===== sECuRE/wochentag-6 =====
main(int a,char**b){char*H[]={"Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"},*d=b[2],*s=" $c-VX\\`]fdZ_";printf("%s\n",H[(31*(a=strchr(s,(d[0]^d[3]^d[2]&127)+9)-s)/12+atoi(b[1])+(a=atoi(b[3])-(a>10))+a/4-a/100+a/400)%7]);}
Score: 252
===== sECuRE/wochentag-crypt =====
main(int a,char**b){char*H[]={"Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"},*m=" zRvT0rQVb297",*o=crypt(b[2],"23")+6;printf("%s\n",H[(31*(a=strchr(m,*o)-m)/12+atoi(b[1])+(a=atoi(b[3])-(a>10))+a/4-a/100+a/400)%7]);}
Score: 244
Compiler-Aufruf: cc wochentag-crypt.c -o wochentag-crypt -lcrypt (=48 Bytes)
==== Perl ====
===== apic =====
use integer;$y=pop;$m=index("anebäpraiunulugepktovez",substr pop,1,2)/2+2;if($m<4){$y--;$m+=12}@t=(Sonn,Mon,Diens,0,Donners,Frei,Sams);$d=$t[(26*$m/10+$y+$y/4-$y/100+$y/400+6+pop)%7];print$d?$d."tag\n":"Mittwoch\n"
Score: 232
===== downhill =====
@m=qw/Ja F Mä Ap Ma Jun Jul Au S O N D/;@t=qw/Sonn Mon Diens Mitt Donners Frei Sams/;($a,$_,$c)=@ARGV;$i=0;while(!/$m[$i]/){$i++;}if($i<2){$i+=14;$c--;}else{$i+=2;}$d=$a+13*$i/5+$c+($c>>2)-int($c/100)+int$c/400+6;print"$t[$d%7]",$d%7==3?woch:tag,"\n";
Score: 269
===== mxf/wochentag-5a =====
($=,$_,$-)=@ARGV;y/DMunipg/KN`106/;$--=/ar/;$=+=$-+$-/4-int$-/100;$=+=$-/400+($_^xS);print+(Mitt,Donners,Frei,Sams,Sonn,Mon,Diens)[$=%=7],$=?tag:woch,$/
Score: 152
===== urs =====
$_-=($_=pop,jriMnlASbvzJu=~((pop=~/./g)x9)[18],$i=$+[0])>11;/../;((Donners,Frei,Sams,Sonn,Mon,Diens,Mittwochx)[(2.6*$i%7-$&*2%7+$&/4%7+$'/.8%7+pop)%7].tagx)=~x;print"$`
"
Score: 171
===== yath =====
",chr unpack"%8A3",pop)>10))=~/../;print+($.=(Sonn,Mon,Diens,0,Donners,Frei,Sams)[((0|2.6*$m-.2)+$'+($'>>2)+($&>>2)-2*$&+pop)%7])?"$.tag":Mittwoch,$/
Score: 181
==== PHP ====
===== Jiska =====
<?$t=array(J,F,z,A,Ma,ni,li,Au,S,O,N,D);$w=array(Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag);for($i=0;$i<12;$i++)preg_match("/$t[$i]/",$argv[2])&&$m=$i+2;$y=$argv[3];if($m<4){$m+=12;$y--;}echo
$w[($argv[1]+(int)(13*$m/5)+$y+(int)($y/4)-(int)($y/100)+(int)($y/400)+6)%7]."\n";?>
Score: 295
==== Python ====
===== kungi =====
import sys;k=int;(n,a,b,c)=sys.argv;l=k(c[:-2]);j=k(c[-2:]);m=b[1:3]
ae,pr,ai,un,ul,ug,ep,kt,ov,ez,an,eb=range(4,16)
if m=='an'or m=='eb':
  j-=1
print 'Samstag Sonntag Montag Dienstag Mittwoch Donnerstag Freitag'.split()[(k(a[:-1])+k(eval(m)*2.6)+j+j/4+l/4-2*l)%7]
Score: 266
===== nic0las =====
import sys
_,d,m,y=sys.argv
m='iMnlASbvzJu'.find((7*m)[18])
y=int(y)-(m>8)
print'Freitag Samstag Sonntag Montag Dienstag Mittwoch Donnerstag'.split()[(13*m/5+int(d[:-1])+y+y/4+y/100*25/4)%7]
Score: 190
==== Ruby ====
===== phil_fry/wochentag-phil.rb =====
a,b,c=$*
y=c.to_i
(m="a[  XZ\\NV".index((b[1]-b[2])%?c)+4)>12&&y-=1
puts (p=%w{Donners Frei Sams Sonn Mon Diens}[(a.to_i+13*m/5+5*(y%?d)/4+(y/=?d)/4-2*y+2)%7])?p+"tag":"Mittwoch"
Score: 181
===== phil_fry/wochentag.rb =====
m=" a[  XZ\\NV".index(($*[1][1]-$*[1][2])%99)
y=$*[2][2..3].to_i
c=$*[2].to_i/100
if m>10
c-=1if y==0
y=(y-1)%100
end
puts %w{Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag}[($*[0].to_i+(2.6*m-0.2).to_i+5*y/4+c/4-2*c)%7]
Score: 236
==== zsh ====
===== sECuRE/wochentag-3.sh =====
l=(locale LC_TIME)
a=$($l|awk "/$2/&&NR>23&&\$0=(NR-2)%12+1" RS=\;)
Y=$[$3-(a>10)]
$l|awk -F\; "NR==2&&\$0=\$$[($1+Y+Y/4-Y/100+Y/400+31*a/12)%7+1]"
Score: 148
===== sECuRE/wochentag-5.sh =====
p=riMnlASbvzJu;a=$p[(i)${2[$[19%$#2]]}];Y=$[$3-(a>10)]
locale LC_TIME|awk -F\; "NR==2&&\$0=\$$[($1+Y+Y/4-Y/100+Y/400+31*a/12)%7+1]"
Score: 132

Latest revision as of 15:20, 29 July 2008

Challenge #3: Wochentagberechnung

"Wie ist der Wochentag eines bestimmten Datums?"

 $ ./wochentag 1. Januar 1970
 Donnerstag
 $

Siehe: http://de.wikipedia.org/wiki/Wochentagsberechnung


Um es ein wenig golfbarer zu machen, rechnen wir nur mit dem gregorianischen Kalender.

Man kann davon ausgehen, dass das Programm immer mit einem legitimen Datum aufgerufen wird. Der Aufruf ist immer in der Form

 \d{1,2}\. (Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember) \d{4}

Die Ausgabe muss:

 (Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag)\n

entsprechen.

sur5r ist der Schiedsrichter dieser Challenge.

Es dürfen keine generischen/speziellen Datumsumrechnungsmodule der Sprache benutzt werden, auch wenn sie Bestandteil der Distribution der Sprache sind.

Das Programm darf nichts nach STDERR schreiben

Test

#!/usr/bin/ruby
def assert_eq(a, b)
   raise Exception.new("assertion failed: "+a+"!="+b) if a!=b
end
assert_eq `#{ARGV[0]} 14. Juli 1789`, "Dienstag\n"
assert_eq `#{ARGV[0]} 23. Mai 1949`, "Montag\n"
assert_eq `#{ARGV[0]} 20. März 1983`, "Sonntag\n"
assert_eq `#{ARGV[0]} 9. November 1989`, "Donnerstag\n"
assert_eq `#{ARGV[0]} 12. Januar 2006`, "Donnerstag\n"
assert_eq `#{ARGV[0]} 1. Januar 2000`, "Samstag\n"
assert_eq `#{ARGV[0]} 1. März 2000`, "Mittwoch\n"
assert_eq `#{ARGV[0]} 1. Januar 1712`, "Freitag\n"
assert_eq `#{ARGV[0]} 1. Februar 2000`, "Dienstag\n"

(Score: 553)

./test.rb ./wochentag

Der Test ist genau dann erfolgreich, wenn keinerlei Ausgabe erfolgt.

Test in Shellscript

Für alle Rubyhasser oder Nicht-Ruby-Haber:

#!/bin/sh
T=$1;t(){ o=$($T $1|tr '\n' X);[ "$o" = "$2X" ]||echo "Assertion failed ($1): ${o/X/} != $2";}
t "14. Juli 1789" "Dienstag"
t "23. Mai 1949" "Montag"
t "20. März 1983" "Sonntag"
t "9. November 1989" "Donnerstag"
t "12. Januar 2006" "Donnerstag"
t "1. Januar 2000" "Samstag"
t "1. März 2000" "Mittwoch"
t "1. Januar 1712" "Freitag"
t "1. Februar 2000" "Dienstag"

(Score: 364)

./test.sh ./wochentag

Inoffizielle, vom Schiri nicht bestätigte Scores

Shellscripting

  • sECuRE / zsh (neu): 132
  • sECuRE / zsh (alt): 148
  • jiska / bash: 227
  • cato / bash: 274
  • CentronX / PowerShell: ca. 800 (Ja, I know!)

Perl

  • mxf: 152
  • urs: 171
  • yath: 181
  • downhill: 245

Python

  • Nicolas / Python: 190
  • Kungi / Python: 257

Ruby

C

  • sECuRE / C: 261 byte (normal), 221 byte (obfuscated, ohne compileraufruf), 133 byte (obfuscated, nicht rules-compliant, ohne compileraufruf)

ABAP

  • SdK / ABAP: 1586

PHP

  • jiska / PHP: 295
  • BugBlue / PHP: 345


Offizielle Ergebnisse

Zunächst mal das Ergebnis der Validierung, Quellcodes kommen noch

C/sECuRE/wochentag-6: ok
C/sECuRE/obfuscated-6: ok
bash/cato/golf3.sh: nicht utf8-clean, bash/cato/golf3.sh: line 5: 09: value too great for base (error token is "09")
bash/jiska/wochentag.sh: ok
Perl/apic/wochentag: ok
Perl/downhill/golf2.pl: ok
Perl/mxf/mxf-wochentag-5a.pl: Failed for 25. Oktober 1746, returned Sonntag instead of Dienstag
Perl/urs/urs.pl: Failed for 21. Februar 1828, returned Mittwoch instead of Donnerstag
Perl/yath/foo.pl: Failed for 5. Dezember 1949, returned Dienstag instead of Montag
PHP/jiska/jiska-wochentag.php: ok
PowerShell/CentronX/pastebin.cmd: wie testen? woher kommt $d? Fehlt da was?
Python/kungi/wochentag.py: nicht utf8-clean, Failed for 12. Januar 1900, returned Donnerstag instead of Freitag
Python/nic0las/wd.py: ok
Ruby/phil_fry/wochentag.rb: ok
Ruby/phil_fry/wochentag-phil.rb: Failed for 5. Dezember 1949, returned Sonntag instead of Montag
zsh/sECuRE/wochentag-3.sh: ok
zsh/sECuRE/wochentag-5.sh: ok

bash

cato
a=(Samstag Sonntag Montag Dienstag Mittwoch Donnerstag Freitag)
case ${2:1:2} in an)m=14;;eb)m=15;;är)m=4;;pr)m=5;;ai)m=6;;un)m=7;;ul)m=8;;ug)m=9;;ep)m=10;;kt)m=11;;ov)m=12;;ez)m=13;; esac
y=$[$3-(m>13)];k=${y:2};j=${y:0:2}
echo ${a[$[(${1/.}+($m*26)/10+k+k/4+j/4+5*j)%7]]}

Score: 275

jiska
t=(J F z A Ma ni li Au S O N D)
w=(SonnX MonX DiensX Mittwoch DonnersX FreiX SamsX)
for((i=0;i<12;i++));do echo $2|grep -q ${t[i]}&&m=$[i+2];done
y=$3
echo ${w[$[m<4&&(m+=12,y--),(${1/./}+13*m/5+y+y/4-y/100+y/400+6)%7]]/X/tag}

Score: 227

C

sECuRE/wochentag-6
main(int a,char**b){char*H[]={"Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"},*d=b[2],*s=" $c-VX\\`]fdZ_";printf("%s\n",H[(31*(a=strchr(s,(d[0]^d[3]^d[2]&127)+9)-s)/12+atoi(b[1])+(a=atoi(b[3])-(a>10))+a/4-a/100+a/400)%7]);}

Score: 252

sECuRE/wochentag-crypt
main(int a,char**b){char*H[]={"Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"},*m=" zRvT0rQVb297",*o=crypt(b[2],"23")+6;printf("%s\n",H[(31*(a=strchr(m,*o)-m)/12+atoi(b[1])+(a=atoi(b[3])-(a>10))+a/4-a/100+a/400)%7]);}

Score: 244

Compiler-Aufruf: cc wochentag-crypt.c -o wochentag-crypt -lcrypt (=48 Bytes)

Perl

apic
use integer;$y=pop;$m=index("anebäpraiunulugepktovez",substr pop,1,2)/2+2;if($m<4){$y--;$m+=12}@t=(Sonn,Mon,Diens,0,Donners,Frei,Sams);$d=$t[(26*$m/10+$y+$y/4-$y/100+$y/400+6+pop)%7];print$d?$d."tag\n":"Mittwoch\n"

Score: 232

downhill
@m=qw/Ja F Mä Ap Ma Jun Jul Au S O N D/;@t=qw/Sonn Mon Diens Mitt Donners Frei Sams/;($a,$_,$c)=@ARGV;$i=0;while(!/$m[$i]/){$i++;}if($i<2){$i+=14;$c--;}else{$i+=2;}$d=$a+13*$i/5+$c+($c>>2)-int($c/100)+int$c/400+6;print"$t[$d%7]",$d%7==3?woch:tag,"\n";

Score: 269

mxf/wochentag-5a
($=,$_,$-)=@ARGV;y/DMunipg/KN`106/;$--=/ar/;$=+=$-+$-/4-int$-/100;$=+=$-/400+($_^xS);print+(Mitt,Donners,Frei,Sams,Sonn,Mon,Diens)[$=%=7],$=?tag:woch,$/

Score: 152

urs
$_-=($_=pop,jriMnlASbvzJu=~((pop=~/./g)x9)[18],$i=$+[0])>11;/../;((Donners,Frei,Sams,Sonn,Mon,Diens,Mittwochx)[(2.6*$i%7-$&*2%7+$&/4%7+$'/.8%7+pop)%7].tagx)=~x;print"$`
"

Score: 171

yath
",chr unpack"%8A3",pop)>10))=~/../;print+($.=(Sonn,Mon,Diens,0,Donners,Frei,Sams)[((0|2.6*$m-.2)+$'+($'>>2)+($&>>2)-2*$&+pop)%7])?"$.tag":Mittwoch,$/

Score: 181


PHP

Jiska
<?$t=array(J,F,z,A,Ma,ni,li,Au,S,O,N,D);$w=array(Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag);for($i=0;$i<12;$i++)preg_match("/$t[$i]/",$argv[2])&&$m=$i+2;$y=$argv[3];if($m<4){$m+=12;$y--;}echo
$w[($argv[1]+(int)(13*$m/5)+$y+(int)($y/4)-(int)($y/100)+(int)($y/400)+6)%7]."\n";?>

Score: 295

Python

kungi
import sys;k=int;(n,a,b,c)=sys.argv;l=k(c[:-2]);j=k(c[-2:]);m=b[1:3]
ae,pr,ai,un,ul,ug,ep,kt,ov,ez,an,eb=range(4,16)
if m=='an'or m=='eb':
 j-=1
print 'Samstag Sonntag Montag Dienstag Mittwoch Donnerstag Freitag'.split()[(k(a[:-1])+k(eval(m)*2.6)+j+j/4+l/4-2*l)%7]

Score: 266

nic0las
import sys
_,d,m,y=sys.argv
m='iMnlASbvzJu'.find((7*m)[18])
y=int(y)-(m>8)
print'Freitag Samstag Sonntag Montag Dienstag Mittwoch Donnerstag'.split()[(13*m/5+int(d[:-1])+y+y/4+y/100*25/4)%7]

Score: 190

Ruby

phil_fry/wochentag-phil.rb
a,b,c=$*
y=c.to_i
(m="a[  XZ\\NV".index((b[1]-b[2])%?c)+4)>12&&y-=1
puts (p=%w{Donners Frei Sams Sonn Mon Diens}[(a.to_i+13*m/5+5*(y%?d)/4+(y/=?d)/4-2*y+2)%7])?p+"tag":"Mittwoch"

Score: 181

phil_fry/wochentag.rb
m=" a[  XZ\\NV".index(($*[1][1]-$*[1][2])%99)
y=$*[2][2..3].to_i
c=$*[2].to_i/100
if m>10
c-=1if y==0
y=(y-1)%100
end
puts %w{Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag}[($*[0].to_i+(2.6*m-0.2).to_i+5*y/4+c/4-2*c)%7]

Score: 236

zsh

sECuRE/wochentag-3.sh
l=(locale LC_TIME)
a=$($l|awk "/$2/&&NR>23&&\$0=(NR-2)%12+1" RS=\;)
Y=$[$3-(a>10)]
$l|awk -F\; "NR==2&&\$0=\$$[($1+Y+Y/4-Y/100+Y/400+31*a/12)%7+1]"

Score: 148

sECuRE/wochentag-5.sh
p=riMnlASbvzJu;a=$p[(i)${2[$[19%$#2]]}];Y=$[$3-(a>10)]
locale LC_TIME|awk -F\; "NR==2&&\$0=\$$[($1+Y+Y/4-Y/100+Y/400+31*a/12)%7+1]"

Score: 132