Debian Codegraph @

sample/trick2022/03-mame/remarks.markdown

raw
1 Execute the program normally.
2
3 ```
4 $ ruby entry.rb
5 ```
6
7 It shakes a string.
8
9 ... Wait! This is not all.
10
11 Next, please apply "leftward gravity" to each letter in the file.
12 IOW, if there is a space to the left of a letter, move it to the left.
13 Here, you may want to use the following command.
14
15 ```
16 $ sed "s/ //g" entry.rb | tee up.rb
17 ```
18
19 This program applies "upward gravity" to each letter in an input text.
20 The following demo will help you understand what this means.
21
22 ```
23 $ cat test.txt
24 $ ruby up.rb test.txt
25 ```
26
27 Now, here's where we come in.
28 Please apply "upward gravity" to entry.rb.
29
30 ```
31 $ ruby up.rb entry.rb | tee left.rb
32 ```
33
34 I think that you already noticed that.
35 This program applies "leftward gravity" to an input text.
36
37 ```
38 $ cat test.txt
39 $ ruby left.rb test.txt
40 ```
41
42 `sed` is no longer required to create `up.rb`; just use `left.rb`.
43
44 ```
45 $ ruby left.rb entry.rb > up.rb
46 ```
47
48 We've come to the final stage.
49 Please apply `left.rb` to `left.rb`.
50
51 ```
52 $ ruby left.rb left.rb | tee horizontal.rb
53 $ ruby horizontal.rb
54 ```
55
56 Of course, it is also possible to apply `up.rb` to `up.rb`.
57
58 ```
59 $ ruby up.rb up.rb | tee vertical.rb
60 $ ruby vertical.rb
61 ```
62
63 Can you tell how they work? Enjoy analyzing!
64
65
66
67 ---
68 Code reading tips (spoiler)
69
70 Some code fragments are highly reused between the programs.
71 For example, note that this program has one code fragment to input a text
72 (`b=$>.read`); `up.rb` and `left.rb` share and use this code fragment.
73 Also, `horizontal.rb` and `vertical.rb` share the fragment `puts'TRICK+2022'`.
74 Sometimes letters in very distant places are reused all over the place.
75
76 Can you tell how it detects if it is already aligned or not yet?
77 Here is a simplified version of the gimmick to switch behavior when
78 left-aligned:
79
80 ```
81 "\ #{puts('not left-aligned yet')}
82 # {puts('left-aligned')}"
83 ```
84
85 And for top-aligned:
86
87 ```
88 "#
89 xx{puts('top-aligned')}
90 x#{puts('not top-aligned yet')}
91 "
92 ```
93
94 It is also necessary to detect "top-left-aligned" and "left-top-aligned".
95 I made tons of subtle adjustments and trial-and-error to create the program.
96 I no longer know precisely how it works.