Codegolf/3

From NoName e.V.
Revision as of 00:44, 27 July 2008 by Sur5r (talk | contribs) (→‎Offizielle Ergebnisse: Erster Teil Quellcodes)
Jump to navigation Jump to search

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/pastebin.rb: Failed for 5. Dezember 1949, returned Sonntag instead of Montag
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: 168

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