From e613cdd53d16327dccf8237f8d5b672804a69eca Mon Sep 17 00:00:00 2001 From: mame Date: Sat, 21 Jul 2018 13:07:50 +0000 Subject: sample/trick2018/: adds the top-five entries of TRICK 2018 See https://github.com/tric/trick2018 for TRICK 2018. Fixes #14930. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64008 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- sample/trick2018/01-kinaba/authors.markdown | 3 ++ sample/trick2018/01-kinaba/entry.rb | 8 +++ sample/trick2018/01-kinaba/remarks.markdown | 55 ++++++++++++++++++++ sample/trick2018/02-mame/authors.markdown | 3 ++ sample/trick2018/02-mame/entry.rb | 15 ++++++ sample/trick2018/02-mame/remarks.markdown | 16 ++++++ sample/trick2018/03-tompng/Gemfile | 2 + sample/trick2018/03-tompng/Gemfile.lock | 15 ++++++ sample/trick2018/03-tompng/authors.markdown | 3 ++ sample/trick2018/03-tompng/entry.rb | 31 ++++++++++++ sample/trick2018/03-tompng/output.txt | 44 ++++++++++++++++ sample/trick2018/03-tompng/remarks.markdown | 19 +++++++ sample/trick2018/03-tompng/trick.png | Bin 0 -> 5661 bytes sample/trick2018/04-colin/authors.markdown | 3 ++ sample/trick2018/04-colin/entry.rb | 2 + sample/trick2018/04-colin/remarks.markdown | 62 +++++++++++++++++++++++ sample/trick2018/05-tompng/authors.markdown | 3 ++ sample/trick2018/05-tompng/entry.rb | 41 +++++++++++++++ sample/trick2018/05-tompng/preview_of_output.png | Bin 0 -> 66800 bytes sample/trick2018/05-tompng/remarks.markdown | 31 ++++++++++++ sample/trick2018/README.md | 16 ++++++ 21 files changed, 372 insertions(+) create mode 100644 sample/trick2018/01-kinaba/authors.markdown create mode 100644 sample/trick2018/01-kinaba/entry.rb create mode 100644 sample/trick2018/01-kinaba/remarks.markdown create mode 100644 sample/trick2018/02-mame/authors.markdown create mode 100644 sample/trick2018/02-mame/entry.rb create mode 100644 sample/trick2018/02-mame/remarks.markdown create mode 100644 sample/trick2018/03-tompng/Gemfile create mode 100644 sample/trick2018/03-tompng/Gemfile.lock create mode 100644 sample/trick2018/03-tompng/authors.markdown create mode 100644 sample/trick2018/03-tompng/entry.rb create mode 100644 sample/trick2018/03-tompng/output.txt create mode 100644 sample/trick2018/03-tompng/remarks.markdown create mode 100644 sample/trick2018/03-tompng/trick.png create mode 100644 sample/trick2018/04-colin/authors.markdown create mode 100644 sample/trick2018/04-colin/entry.rb create mode 100644 sample/trick2018/04-colin/remarks.markdown create mode 100644 sample/trick2018/05-tompng/authors.markdown create mode 100644 sample/trick2018/05-tompng/entry.rb create mode 100644 sample/trick2018/05-tompng/preview_of_output.png create mode 100644 sample/trick2018/05-tompng/remarks.markdown create mode 100644 sample/trick2018/README.md (limited to 'sample') diff --git a/sample/trick2018/01-kinaba/authors.markdown b/sample/trick2018/01-kinaba/authors.markdown new file mode 100644 index 0000000000..d0df0b379d --- /dev/null +++ b/sample/trick2018/01-kinaba/authors.markdown @@ -0,0 +1,3 @@ +* kinaba + * twitter.com/kinaba + * cctld: jp diff --git a/sample/trick2018/01-kinaba/entry.rb b/sample/trick2018/01-kinaba/entry.rb new file mode 100644 index 0000000000..eb8284d5ab --- /dev/null +++ b/sample/trick2018/01-kinaba/entry.rb @@ -0,0 +1,8 @@ +alias BEGIN for unless def class +super true or return defined? next +break while begin undef do end +rescue then retry else undef module +nil ensure case if yield __LINE__ +self and redo elsif not __FILE__ +alias END in end when __ENCODING__ +end until false end diff --git a/sample/trick2018/01-kinaba/remarks.markdown b/sample/trick2018/01-kinaba/remarks.markdown new file mode 100644 index 0000000000..a1a05bfd73 --- /dev/null +++ b/sample/trick2018/01-kinaba/remarks.markdown @@ -0,0 +1,55 @@ +### Remarks + +Just run it with no argument: + + ruby entry.rb + +(Anyway it is just a no-op program. The above command only verifies +that entry.rb is a valid Ruby program.) + +I confirmed the following implementations/platforms: + +* ruby 2.5.0p0 (2017-12-25 revision 61468) [x64-mingw32] + +### Description + +First, look at + +https://docs.ruby-lang.org/ja/latest/doc/spec=2flexical.html#reserved + +and then, look at entry.rb. + +The source code of entry.rb consists only of reserved words of Ruby, +and all the reserved words are used in the code, in a way that the code +forms a valid Ruby program. No compile error, no warning, or no runtime error. + + +### Internals + +Difficult (and interesting) points of the theme are: + +* Since many of the reserved words define program structures, we cannot + use them independently. For instance, `retry` must be inside `rescue`, + or `break`/`next`/`redo` must be inside a looping construct. + Or, jump-out statements cannot occur at a position that requires a + value; `if return then true end` is a "void value expression" syntax error. +* Inserting newlines for each 6 word (to match with the spec html) is also + an interseting challenge, since Ruby is sensitive to newlines. + +Tricks used in the code are: + +* def/alias/undef can take even reserved words as parameters. + That is, `def class ... end` defines a method named `class`. + The feature is crucial since otherwise `BEGIN` etc inevitably + introduces non-reserved tokens (like `{}`). +* `defined?` can take some reserved words too (which I didn't know + until trying to write this program.) +* "void value expression" can be avoided by using `or` or `and`. + `if begin return end then true end` is a syntax error, but + `if begin false or return end then true end` is not. + + +### Limitation + +Sad to say that it's not a "perfect pangram". +It uses 'alias' and 'undef' twice, and 'end' 4 times. diff --git a/sample/trick2018/02-mame/authors.markdown b/sample/trick2018/02-mame/authors.markdown new file mode 100644 index 0000000000..0e420fdf5d --- /dev/null +++ b/sample/trick2018/02-mame/authors.markdown @@ -0,0 +1,3 @@ +* Yusuke Endoh + * mame@ruby-lang.org + * cctld: jp diff --git a/sample/trick2018/02-mame/entry.rb b/sample/trick2018/02-mame/entry.rb new file mode 100644 index 0000000000..cc4ef9cbc4 --- /dev/null +++ b/sample/trick2018/02-mame/entry.rb @@ -0,0 +1,15 @@ +'';eval(r=%q(->z{r="'';eval(r=\ +%q(#{r}))[%q`#{z}`]";i=-040;30. +times{|n|(15+n%2*15-n/2).times{ +r<["t]];};o[1,?\n*8];ex"-}eac +1Hl<1[-1]*2*t=n%2];o[14-n,0)mvk +8M$<4,?\n];15.times{|n|;o[35ie2 +!Pss.slice!(0,1)+x;sleep(0.0t;0 +'W=%q"<<95<<$s<<95;o=->n,x{n.'1 +;@[2]}|\e../,%@s="'%trick2018!8 +eval$s=%q_eval($s.gsub!(/#{%@`] diff --git a/sample/trick2018/02-mame/remarks.markdown b/sample/trick2018/02-mame/remarks.markdown new file mode 100644 index 0000000000..88b32c205a --- /dev/null +++ b/sample/trick2018/02-mame/remarks.markdown @@ -0,0 +1,16 @@ +This program quines with animation. + +``` +$ ruby entry.rb +``` + +Of course, the output is executable. + +``` +$ ruby entry.rb > output +$ ruby output +``` + +Note, we don't cheat. This program uses escape sequences just for moving the cursor. It doesn't use attribution change nor overwrite to hide any code. + +The program is crafted so that it works in two ways; it works as a normal program text, and, it also works when it is rearranged in a spiral order. Some parts of the code are actually overlapped. diff --git a/sample/trick2018/03-tompng/Gemfile b/sample/trick2018/03-tompng/Gemfile new file mode 100644 index 0000000000..983f2f3bd4 --- /dev/null +++ b/sample/trick2018/03-tompng/Gemfile @@ -0,0 +1,2 @@ +ruby '2.5.0' +gem 'chunky_png' diff --git a/sample/trick2018/03-tompng/Gemfile.lock b/sample/trick2018/03-tompng/Gemfile.lock new file mode 100644 index 0000000000..6f54c44066 --- /dev/null +++ b/sample/trick2018/03-tompng/Gemfile.lock @@ -0,0 +1,15 @@ +GEM + specs: + chunky_png (1.3.8) + +PLATFORMS + ruby + +DEPENDENCIES + chunky_png + +RUBY VERSION + ruby 2.5.0p0 + +BUNDLED WITH + 1.16.1 diff --git a/sample/trick2018/03-tompng/authors.markdown b/sample/trick2018/03-tompng/authors.markdown new file mode 100644 index 0000000000..26ebe24da6 --- /dev/null +++ b/sample/trick2018/03-tompng/authors.markdown @@ -0,0 +1,3 @@ +* Tomoya Ishida (tompng) + * tomoyapenguin@gmail.com + * cctld: jp diff --git a/sample/trick2018/03-tompng/entry.rb b/sample/trick2018/03-tompng/entry.rb new file mode 100644 index 0000000000..f6de44b6be --- /dev/null +++ b/sample/trick2018/03-tompng/entry.rb @@ -0,0 +1,31 @@ +X=[];class String def-@;replace ?-+self end;def-a;X.reject!{|x|x. +__id__==__id__};a.replace(self+?-+a) end end;at_exit{eval C=(Zlib +.inflate Integer((X*?-).tr(?-,'').tr('q-z','0-9'),26).digits(256) +.pack'C*')};def method_missing n;(X<r,z,dr,dz{;r-=w/2.0;z*=2;r2,z2=r+dr,z+dz*2;if r>0||r2> + 0;r=[0,r].max;r2=[0,r2].max;16.times{|i|m=Math;p=m::PI/8;;c,s=m.cos(t=i*p),m.sin(t) + c2,s2=m.cos(t=(i+1)*p),m.sin(t);t-=p/2;[[0,1,2],[0,2,3]].map{|a|b.push [:facet,'n'+ + + 'ormal',dz*m.cos(t),dz*m.sin(t),-dr]*' ','outer loop',a.map{|i|'v'+ + ++ "ertex #{[[r*c,r*s,z],[r*c2,r*s2,z],[r2*c2,r2*s2,z2],[r2* + +c, r2*s,z2]][i]*' '}"},:endloop,:endfacet}}end};(0...h). + map{| y|w.times{|x|[X[y-1][x]||a[x,y,1,0],X[y+1][x]|| + a[x+1,y+ + 1,-1,0],X[ + y][x-+1]||a[ + x,y+1,0,-1],X[y + ][x++1]||a[x+1,y, + 0,1]]if X[y][x]}} + s=[b,'end'+b[0]]* + $/;File.write(f, + s);X.replace( + []);end + +gen3d 'wine_glass.stl' do + l--ww------------------ww--l + l--ww------------------ww--l + l--ww++++++++++++++++++ww--l + l--ww++++++++++++++++++ww--l + l--ww++++++++++++++++++ww--l + l--ww++++++++++++++++++ww--l + l---ww++++++++++++++++ww---l + l----www++++++++++++www----l + l------www++++++++www------l + l--------wwwwwwwwww--------l + l-----------wwww-----------l + l------------ww------------l + l------------ww------------l + l------------ww------------l + l-----------wwww-----------l + l---------wwwwwwww---------l + l----wwwwwwwwwwwwwwwwww----l +end diff --git a/sample/trick2018/05-tompng/preview_of_output.png b/sample/trick2018/05-tompng/preview_of_output.png new file mode 100644 index 0000000000..db511ee2f3 Binary files /dev/null and b/sample/trick2018/05-tompng/preview_of_output.png differ diff --git a/sample/trick2018/05-tompng/remarks.markdown b/sample/trick2018/05-tompng/remarks.markdown new file mode 100644 index 0000000000..17be56b61f --- /dev/null +++ b/sample/trick2018/05-tompng/remarks.markdown @@ -0,0 +1,31 @@ +### Remarks + +Just run it with no argument: + + ruby entry.rb + +I confirmed the following implementations/platforms: + +* ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16] +* ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16] +* ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16] + +### Description + +This program will generate `wine_glass.stl`, a 3D data file(STL format) of a wine glass. +You can change the shape by modifying the DSL part. +For sake cup: +```ruby +gen3d 'ochoko.stl' do + l------------------------l + l-ww------------------ww-l + l-ww------------------ww-l + l-ww++++++++++++++++++ww-l + l-ww++++++++++++++++++ww-l + l--ww++++++++++++++++ww--l + l---wwww++++++++++wwww---l + l----wwwwwwwwwwwwwwww----l + l----www----------www----l +end +``` +`+` and `-` are the same meaning(just for apperance) diff --git a/sample/trick2018/README.md b/sample/trick2018/README.md new file mode 100644 index 0000000000..8e5729f151 --- /dev/null +++ b/sample/trick2018/README.md @@ -0,0 +1,16 @@ +This directory contains the award-winning entries of +the 3rd Transcendental Ruby Imbroglio Contest for rubyKaigi (TRICK 2018). + +THESE ARE BAD EXAMPLES! You must NOT use them as a sample code. + +* 01-kinaba/entry.rb: "Most reserved" - **Gold award** +* 02-mame/entry.rb: "Best spiral" - **Silver award** +* 03-tompng/entry.rb: "Best png viewer" - **Bronze award** +* 04-colin/entry.rb: "Best one-liner" - 4th prize +* 05-tompng/entry.rb: "Most three-dimentional" - 5th prize + +These files are licensed under MIT license. + +For the contest outline and other winning entries, see: + +https://github.com/tric/trick2018 -- cgit v1.2.3