Skip to content

Commit

Permalink
sample/trick2022/: adds the top-three entries of TRICK 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Dec 23, 2022
1 parent 1d3bfd8 commit e8b0180
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sample/trick2022/01-tompng/Gemfile
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gem 'matrix'
13 changes: 13 additions & 0 deletions sample/trick2022/01-tompng/Gemfile.lock
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
matrix (0.4.2)

PLATFORMS
x86_64-darwin-20

DEPENDENCIES
matrix

BUNDLED WITH
2.3.3
3 changes: 3 additions & 0 deletions sample/trick2022/01-tompng/authors.markdown
@@ -0,0 +1,3 @@
* Tomoya Ishida (tompng)
* tomoyapenguin@gmail.com
* cctld: jp
40 changes: 40 additions & 0 deletions sample/trick2022/01-tompng/entry.rb
@@ -0,0 +1,40 @@
eval((s=%~c=(0..35
).map{s[2*_1+1]}*'';class$Inte
ger;def$quXinclude(Math ;spXo(a)=self*
a.pow(87X=h=32.chr;g=PI/480;ls=(sp*31X,89)%89;
def$abX+'eval((s=%'+(n=? .next)+s*88.chr+[nXs()=[a
=self%X+'.split(',sp*25+'?'+88.chr+');(0..36).mapX89,89-
a].miX{s[2*_1].split}',sp*31+".join.tr('$',$/)))"]*$/)Xn;end
;reqX.split$/;trap(:INT){puts;exit};q=->t,i{a,y=((t+i*99)Xuire
'matrX%960). ivmod(80);[(a*(7+i)+i*23)%79+(y+a)/(5+i%4)%2,39Xix';1
5.tiX-y/2]};p=->t,u{a=->b,c{(0..5).sum{(u%2-1)*E**(t*(b+c*_1)*gXmes{
|i,*X.i+ i*u+=5+sin(u*u))}};x,z=a[5,3]. 5,3].rect;x+=y.Xv|z=
*?!Xi a[19,4];z+=w;r=(4+(x.abs+z.i).ab };t=(0..959).fX..?
W,?Xind{|t|(0..29).all?{x,y=q[t,_1];(x 2||h=ls[y][x]X[,*
?]..X[/[^!-}]/]}};h=($**h+h).chr;eval( []} ->(x,yX?};a
=(0X,a,b){x=x*36+39.5;y=19.5-y*18;b*=1 |i|((yX..1
34)X-b).ceil..y+b).map{|j|((x-i)/a+(y j)/ .times{X.ma
p{zXx,z=p[t,_1];l=u```=0;while``````(l<1)``; u+```=0 ;d=x-y;X.in
dex(Xl+=(d.abs+(z-w``)``.i).ab``s*1.``1 ;x``,z=y``,w;o[v``=``x.r d.imag/Xc[i+
15*Xd.abs*l*sin(2*``l-t``*g*80``-_1) l*(``1-l)/``6,a``=l*( -l)**2*0.X_1]
)};X7,a*2]&&o[v,z,``0.0``3,l**`` ``times``{|i``|(8+i).times{|Xw=*
MatXj|o[sin(i)/2+````` ```sin( `/2.0`````````)*j/200,j*0.0Xrix
[*(X5-1,0.02,0.1]``}} ``. q[t,``_1];m``[y][x]= };i=-X0..
44).X1;$><<(['%%','[H .map{|j|(0..79).map{|k|x=(Xmap{
|i,X -39.5)/35.8;y=( i+=1;m[j][k]?h:c[i]):ls[j]X*b|
v<<X[k];}*''}*$/<<0) 1)%9 te"`")#qv.jSaL{=;q(Q}4fXa.z
ip(Xjs(:#tK`Jm))FKO /A9(2'%iorvf7 eEa0uV xv+Q@qUU](L@&Py .1v'X0..
).suXydSEH{-GI|-5(,z G5evpq,[b50 D[ t {on,I?VStS`?G@LoqFCXm{|j
,k|Xj1.QnxKz!mH%o# )b2Seut,]! 48 lBieJGi 5jeNPD#b}H3X-(p
=(iXaVz#8*+US,hgF 5#6]y-` 4hy HN hF75WjD!0IxJ$sX+k)
.powX+UP"cNUE9- G< tHvV;Ib <-s U T ? vlE xylg=x#X(i+k
,88)XV9u$9lKb9 @C do7+-w >l { v9 { P l ga%]AK<e&'X+1)*
(j||(X4ifK/6S+ k} @@*a} 6rS xn"Q[M 8 `|g>$#BrjXb<<p;
0))}XtbDp'Kc t2 Dat9C s C rL+ g,j]Tf B< eMI+zzkWX;b}]
.lup.XtVP<ak IM E/+)B jwv uB (Twqed D* dyf_dT7Xsolve
(v);13Xn:8 #_ RiSTO, [Fk m O]O#"+ a_ cT_.X5.time
s{c[i+X e5 T`FBEC q*f 2 o@{a<eUG aW PX15*_1]
=z[a[_1]X z_@`nll 7F1 2 [=^uS0z^ 6X||w.shif
t]}};eval(Xfg K#R N bp-E_Xc)~.split(
?X);(0..36).map{s[2*_1].split}
.join.tr('$',$/)))
51 changes: 51 additions & 0 deletions sample/trick2022/01-tompng/remarks.markdown
@@ -0,0 +1,51 @@
### Remarks

Just run it with no argument:

ruby entry.rb

Or run it with one non-ascii half-width character argument:

ruby entry.rb ⬮
ruby entry.rb 𓆡

I confirmed the following implementations/platforms:

* ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
* ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin20]

### Description

This program is an aquatic quine.
Some characters in the code are overwritten with `" "`, but this program can restore the missing parts.
Every frame of this animation is an executable ruby program that let fishes start swimming again from their current position.

### Internals

#### Error Correction

Error correction is performed for each block of length 135.
It consists of 89 kinds of characters(`[*('!'..'W'), '[', *(']'..'}')]`) and satisfies the following constraint.

```
matrix(size: 45x135) * block_vector(size: 135) % 89 == zero_vector(size: 45)
```

To restore the missing characters in the block, we need to solve a linear equation problem in modulo 89.
This can be achieved by using bundled gem 'matrix' and overwriting some methods.

```ruby
require 'matrix'
matrix = Matrix[[3, 1, 4], [1, 5, 9], [2, 6, 5]]
class Integer
def quo(x) = self * x.pow(87, 89) % 89 # Fermat's little theorem. 89 is a prime number.
def abs() = [self % 89, 89 - self % 89].min # To avoid division by multiple of 89.
end
answer = matrix.lup.solve([1, 2, 3]) #=> Vector[24, 42, 83]
(matrix * answer).map { _1 % 89 } #=> Vector[1, 2, 3]
```
#### Resuming Animation
The entire animation of this fish tank is a loop of 960 frames.
This program uses position of the floating bubbles to detect current frame number from the executed source code.
3 changes: 3 additions & 0 deletions sample/trick2022/02-tompng/authors.markdown
@@ -0,0 +1,3 @@
* Tomoya Ishida (tompng)
* tomoyapenguin@gmail.com
* cctld: jp
32 changes: 32 additions & 0 deletions sample/trick2022/02-tompng/entry.rb
@@ -0,0 +1,32 @@
q=->{!sleep _1/1e2};p=(
c=0..2).map{[_1/9r ,0,5**_1.i/3,1,0]}
require'socket';puts'op' "en http://localhost:#{(
w=TCPServer.new$*[0]||0).addr[1]}";Thread.new{q[2];f=[-1
]*s=3;t=Time.now.to_f;p.select!{0<_1[3]=[_1[3]+_1[4]/8.0,1
].min};9.times{h=p.map{[2**(_1*t.i)/_4**0.5/(1+Math.sin(2*t-
9*_1%2)**32/16),_2+_4*( _3-_2)]};r=[s*3/2,84].min;g=->{x,y=
(s*(1+_1+1i)/2).rect;x<0 ||x>=s-1||y<0||y>=s-1?0:((l=f[y+1])[
x+1]*(a=x%1)+(1-a)*l[x] )*(b=y%1)+(1-b)*((l=f[y])[x+1]*a+(1-
a)*l[x])};f=(1..r).map {|y|(1..r).map{|x|z=1.5+1.5i-3.0*(y
.i+x)/r;[h.sum{g[_1.*z +_2]}*0.9,1].min}};s=r};c=f.flatten
redo};loop{s=w.accept ; Thread.new{r=s.gets;h='HTTP/1.1 '+
"200 OK\r\nContent-" 'T' "ype:text/html\r\n\r\n";r['/ ']?s.
<<(h+'<style>ifram' 'e{' 'opacity:0;height:0;}input{wid'+
'th:252px;}</styl' 'e>' '<form target="i"><input src="'+
"g#{rand}\" type" '="im' 'age"><iframe name="i"></ifra'+
'me></form>'):r ['/g'] ?(h[/:.+l/]=?:'image/gif';s<<
h+'GIF8' '7a'+[84,
84,246,0,*(0..383).map {15*_1. /(383r)**(3-_1%
3)*17}].pack('v3c*'); loop{ s<<[67434785,5,
44,84,84,7,c.map{_1* 127} .each_slice(126
).map{[127,128,*_1 ] .pack'c*'}*'',
1,129].pack('V3x' 'v2na*c2x');q[
5];q.[]1while(r ==r=c)}):(x,y,
z=r.scan(/\d+/).map{_1.to_f/
126-1};z&&p<<[rand-0.5,(
z=x+y.i)*1.5,z/(z.
abs+0.9),0,-p[
-3][4]=-1]
s.<<h);s
.close
}}
32 changes: 32 additions & 0 deletions sample/trick2022/02-tompng/remarks.markdown
@@ -0,0 +1,32 @@
### Remarks

1. Run it with `ruby entry.rb 8080`

2. Open "http://localhost:8080"

3. Click on the screen and interact with it

I confirmed the following implementations/platforms:

* Ruby Version
* ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin20]
* Browser
* Chrome(macOS, Android)
* Firefox(macOS)
* Edge(macOS)

### Description

This program is an HTTP server that provides a fractal creature playground.
You can see the heartbeat of a mysterious fractal creature. Clicking on the screen will change the shape of the creature.
Surprisingly, this interactive webpage is built without JavaScript.

### Internals

Fractal: Iterated function system
Rendering from server: Streaming animated GIF
Sending click event to server: `<input type="image" src="streaming.gif">` with `<form target="invisible_iframe">`

### Limitations

Does not work on Safari and iOS.
3 changes: 3 additions & 0 deletions sample/trick2022/03-mame/authors.markdown
@@ -0,0 +1,3 @@
* Yusuke Endoh
* mame@ruby-lang.org
* cctld: jp
27 changes: 27 additions & 0 deletions sample/trick2022/03-mame/entry.rb
@@ -0,0 +1,27 @@
2022;"#

.chars} {puts'TRICK+2022'
\ { ;
#';$><< b
?!
};#{s=' ' # 0
! s[0]? ( b=$<.read ;'
} ub( ,''}
';a= ''<<32
b.lines {puts( ?.. *(
b.size) .gsub(/./) {
b.sub!( /^#$`\K(\S)
/x,a)?$1:a }
.rstrip)}):
( [ 12,1,12,11].
cycle { | i | t = ( s *
10<<
10)*
10+ %(\e[A)*
10
10. times{t[i*
_1 ] = 'TRICK+2022'[
_1 ] };$><<t
sleep 1})
}"
96 changes: 96 additions & 0 deletions sample/trick2022/03-mame/remarks.markdown
@@ -0,0 +1,96 @@
Execute the program normally.

```
$ ruby entry.rb
```

It shakes a string.

... Wait! This is not all.

Next, please apply "leftward gravity" to each letter in the file.
IOW, if there is a space to the left of a letter, move it to the left.
Here, you may want to use the following command.

```
$ sed "s/ //g" entry.rb | tee up.rb
```

This program applies "upward gravity" to each letter in an input text.
The following demo will help you understand what this means.

```
$ cat test.txt
$ ruby up.rb test.txt
```

Now, here's where we come in.
Please apply "upward gravity" to entry.rb.

```
$ ruby up.rb entry.rb | tee left.rb
```

I think that you already noticed that.
This program applies "leftward gravity" to an input text.

```
$ cat test.txt
$ ruby left.rb test.txt
```

`sed` is no longer required to create `up.rb`; just use `left.rb`.

```
$ ruby left.rb entry.rb > up.rb
```

We've come to the final stage.
Please apply `left.rb` to `left.rb`.

```
$ ruby left.rb left.rb | tee horizontal.rb
$ ruby horizontal.rb
```

Of course, it is also possible to apply `up.rb` to `up.rb`.

```
$ ruby up.rb up.rb | tee vertical.rb
$ ruby vertical.rb
```

Can you tell how they work? Enjoy analyzing!



---
Code reading tips (spoiler)

Some code fragments are highly reused between the programs.
For example, note that this program has one code fragment to input a text
(`b=$>.read`); `up.rb` and `left.rb` share and use this code fragment.
Also, `horizontal.rb` and `vertical.rb` share the fragment `puts'TRICK+2022'`.
Sometimes letters in very distant places are reused all over the place.

Can you tell how it detects if it is already aligned or not yet?
Here is a simplified version of the gimmick to switch behavior when
left-aligned:

```
"\ #{puts('not left-aligned yet')}
# {puts('left-aligned')}"
```

And for top-aligned:

```
"#
xx{puts('top-aligned')}
x#{puts('not top-aligned yet')}
"
```

It is also necessary to detect "top-left-aligned" and "left-top-aligned".
I made tons of subtle adjustments and trial-and-error to create the program.
I no longer know precisely how it works.
13 changes: 13 additions & 0 deletions sample/trick2022/03-mame/test.txt
@@ -0,0 +1,13 @@
T
R
I
C
K
|
|
|
|
2
0
2
2
14 changes: 14 additions & 0 deletions sample/trick2022/README.md
@@ -0,0 +1,14 @@
This directory contains the award-winning entries of
the 4th Transcendental Ruby Imbroglio Contest for rubyKaigi (TRICK 2022).

THESE ARE BAD EXAMPLES! You must NOT use them as a sample code.

* 01-tompng/entry.rb: "Best fishbowl" -- Tomoya Ishida (tompng)
* 02-tompng/entry.rb: "Most interactive code" -- Tomoya Ishida (tompng)
* 03-mame/entry.rb: "Most anti-gravity" -- Yusuke Endoh

These files are licensed under MIT license.

For the contest outline and other winning entries, see:

https://github.com/tric/trick2022

0 comments on commit e8b0180

Please sign in to comment.