This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99_bottles.mt
212 lines (189 loc) · 4.47 KB
/
99_bottles.mt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// There are four cells we care about in the top-level tape:
// - tens, ones, [scratchpad], last bottle?
// The scratchpad should always be null after any operation.
!{=9}>!{=9}>
[
!{print bottle count}
!{" bottles of beer on the wall"}
!{newline}
!{print bottle count}
!{" bottles of beer"}
!{newline}
!{"Take one down, pass it around"}
!{newline}
// Break out of the loop if this is the last bottle.
>(n<|<
!{dec bottles}
// Check whether this is the last bottle.
>f{ << !{=1?} ( < !{=0?} ) }<
!{print bottle count}
!{" bottles of beer on the wall"}
!{newline}
!{newline}
])
!N !o !{" bottles of beer on the wall"}
!{newline}
!{newline}
!N !o !{" bottles of beer on the wall"}
!{newline}
!N !o !{" bottles of beer"}
!{newline}
!G !o !_ !t !o !_ !t !h !e !_ !s !t !o !r !e !, !_ !b !u !y !_ !s !o !m !e !_ !m !o !r !e
!{newline}
!9 !9 !{" bottles of beer on the wall"}
!{newline}
@ dec bottles {
// Check whether the ones place is 0.
f{ < !{=0?} }(
// Decrement the tens place.
<< !{dec}
// Reset the ones place to 9.
> !{=9}
>
|
// Decrement the ones place.
< !{dec}
>
)n
}
@ print bottle count {
// Check whether the tens place is not 0.
f{ << !{=0?} }(n|
// If it is not 0, print it.
<< !{print digit} >>
)
// Print the ones digit.
< !{print digit} >
}
@ " bottles of beer" {
// Print " bottle"
!_ !b !o !t !t !l !e
// Only print "s" if this is not the last bottle.
>(<|< !s )
// Print " of beer"
!_ !o !f !_ !b !e !e !r
}
@ " bottles of beer on the wall" {
!{" bottles of beer"}
!_ !o !n !_ !t !h !e !_ !w !a !l !l
}
@ "Take one down, pass it around" {
!T !a !k !e !_
// If this is the last bottle ...
>(<
// ... then print "it"
!i !t
|<
// ... otherwise print "one"
!o !n !e
)
!_ !d !o !w !n !,
!_ !p !a !s !s
!_ !i !t
!_ !a !r !o !u !n !d
}
@ print digit {
// Ascii '0' is 0x30, '1' is 0x31, ..., '9' is 0x39. So just output 0011 as
// the first four bits, and then the next four bits come straight from the
// number.
e
// Output 0x39 = bin 0011.
>oo<oo
// Output the highest bit. (If the bit is null, do not enter.)
<<<(eox|o)
// Output the next bit. (If the bit is null, do not enter.)
>(eox|o)
// etc.
>(eox|o)
>(eox|o)
x
}
@ dec {
e
// If nonzero ...
> f{ <x !{=0?} } (n<|<
// While the current bit is 0, set it to 1 and move left.
[(e(x|exx<]))
// We have now found the rightmost 1 bit. Zero it.
enx
!{_ trim leading zeros}
)
x
}
@ _ trim leading zeros {
// Find left end.
[<(])
// Null all leading zeros, as long as there are bits.
[>(e(x|xn])
// If we haven't run out of bits, find the right end.
[>(])<
|
// If we have, make a zero bit instead of null.
ex
)
}
/// Predicate functions destroy the current cell, returning truthy if true and
/// null if false.
@ =0? {
f{ // Fork
e
// Check that the lowest bit is 0 ...
e(|x
// ... and that the next bit doesn't exist
<(|
nx // Return true.
|n) // Else return false.
|n) // Else return false.
}
}
@ =1? {
f{ // Fork
e
// Check that the lowest bit is 1 ...
e(x
// ... and that the next bit doesn't exist
<(|
nx // Return true.
|n) // Else return false.
|n) // Else return false.
}
}
/// Set the current cell to an integer with the value 9 (bin 1001).
@ =9 {
e
eexx // highest bit
>ex
>ex
>eexx // lowest bit
x
}
// We need lowercase "gjqvz" and the only capital letters we need are "GNT".
// Each of these assumes `ex>`.
@ , { oo<o>o<oo>oo }
@ 9 { oo<ooo>oo<o> }
@ newline { oooo<o>o<o>o }
@ _ { oo<o>ooooo }
@ a { o<oo>oooo<o> }
@ b { o<oo>ooo<o>o }
@ c { o<oo>ooo<oo> }
@ d { o<oo>oo<o>oo }
@ e { o<oo>oo<o>o<o> }
@ f { o<oo>oo<oo>o }
@ G { o<o>ooo<ooo> }
@ h { o<oo>o<o>ooo }
@ i { o<oo>o<o>oo<o> }
@ k { o<oo>o<o>o<oo> }
@ l { o<oo>o<oo>oo }
@ m { o<oo>o<oo>o<o> }
@ N { o<o>oo<ooo>o }
@ n { o<oo>o<ooo>o }
@ o { o<oo>o<oooo> }
@ p { o<ooo>oooo }
@ r { o<ooo>oo<o>o }
@ s { o<ooo>oo<oo> }
@ T { o<o>o<o>o<o>oo }
@ t { o<ooo>o<o>oo }
@ u { o<ooo>o<o>o<o> }
@ w { o<ooo>o<ooo> }
@ x { o<oooo>ooo }
@ y { o<oooo>oo<o> }