-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcister
executable file
·226 lines (215 loc) · 5.75 KB
/
cister
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env ruby
require 'rasem'
require 'pry'
SPACE = 20
TABLET = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: 10,
k: 11,
l: 12,
m: 13,
n: 14,
o: 15,
p: 16,
q: 18,
r: 19,
s: 100,
t: 200,
u: 300,
v: 400,
w: 500,
x: 600,
y: 700,
z: 800,
" ": 900
}.freeze
# processing
print 'What is your message? '
message = gets
puts message
def cister(message)
message.downcase
.gsub(/\W/, ' ')
.squeeze(' ')
.strip
.chars
.map do |c|
n = TABLET[c.to_sym] * 10
r = rand(n)
[n - r, r].shuffle
end.flatten
end
# Extract units, tens, thousands, hundreds
def digits(num)
cs = num.to_s.chars
[].tap do |o|
while cs.size > 0
c = cs.shift
o << (c + Array.new(cs.length) { |_i| 0 }.join).to_i
end
end
end
def snail(array)
array.empty? ? [] : array.shift + snail(array.transpose.reverse)
end
# Insert at random place symbols.
# Every symbol before the 9999 symbol
# should be ignored to uncipher the message
def inject_evil_symbols(array)
array.insert(
rand(0..array.length),
[rand(9999), 9999]
).flatten!
end
# Find the perfect square
def perfect_square?(x)
Math.sqrt(x) % 1 == 0
end
# Units
# line 50, 0, 100, 0 # 1579 -
# line 50, 50, 100, 0 # 45 /
# line 100, 0, 100, 50 # 6789 |
# line 50, 0, 100, 50 # 3 \
# line 50, 50, 100, 50 # 289 _
# # Tens
# line 50, 0, 0, 0 # 1579 -
# line 0, 50, 0, 0 # 6789 |
# line 50, 50, 0, 50 # 289 _
# line 50, 50, 0, 0 # 45 \
# line 50, 0, 0, 50 # 3 /
# # Hundreds
# line 50, 50, 100, 50 # 289 -
# line 100, 50, 100, 100 # 1579 _
# line 50, 100, 100, 100 # 6789 |
# line 50, 50, 100, 100 # 45 \
# line 50, 100, 100, 50 # 3 /
# # Thousands
# line 50, 50, 0, 50 # 289 -
# line 50, 50, 0, 100 # 45 /
# line 0, 50, 0, 100 # 6789 |
# line 50, 100, 0, 100 # 1579 _
# line 50, 100, 0, 50 # 3 \
def symbol(ds, index, y = 0)
# Line that always appear
x = (100 + SPACE) * index
line(50, 0, 50, 100).translate(x, y)
# Lines that may or may not appear depending of the digit
ds.each do |num|
case num
when 1
line(50, 0, 100, 0).translate(x, y) # 1579 -
when 2
line(50, 50, 100, 50).translate(x, y) # 289 _
when 3
line(50, 0, 100, 50).translate(x, y) # 3 \
when 4
line(50, 50, 100, 0).translate(x, y) # 45 /
when 5
line(50, 0, 100, 0).translate(x, y) # 1579 -
line(50, 50, 100, 0).translate(x, y) # 45 /
when 6
line(100, 0, 100, 50).translate(x, y) # 6789 |
when 7
line(50, 0, 100, 0).translate(x, y) # 1579 -
line(100, 0, 100, 50).translate(x, y) # 6789 |
when 8
line(100, 0, 100, 50).translate(x, y) # 6789 |
line(50, 50, 100, 50).translate(x, y) # 289 _
when 9
line(50, 0, 100, 0).translate(x, y) # 1579 -
line(100, 0, 100, 50).translate(x, y) # 6789 |
line(50, 50, 100, 50).translate(x, y) # 289 _
when 10
line(50, 0, 0, 0).translate(x, y) # 1579 -
when 20
line(50, 50, 0, 50).translate(x, y) # 289 _
when 30
line(50, 0, 0, 50).translate(x, y) # 3 /
when 40
line(50, 50, 0, 0).translate(x, y) # 45 \
when 50
line(50, 50, 0, 0).translate(x, y) # 45 \
line(50, 0, 0, 0).translate(x, y) # 1579 -
when 60
line(0, 50, 0, 0).translate(x, y) # 6789 |
when 70
line(0, 50, 0, 0).translate(x, y) # 6789 |
line(50, 0, 0, 0).translate(x, y) # 1579 -
when 80
line(50, 50, 0, 50).translate(x, y) # 289 _
line(0, 50, 0, 0).translate(x, y) # 6789 |
when 90
line(0, 50, 0, 0).translate(x, y) # 6789 |
line(50, 0, 0, 0).translate(x, y) # 1579 -
line(50, 50, 0, 50).translate(x, y) # 289 _
when 100
line(50, 100, 100, 100).translate(x, y) # 1579 _
when 200
line(50, 50, 100, 50).translate(x, y) # 289 -
when 300
line(50, 100, 100, 50).translate(x, y) # 3 /
when 400
line(50, 50, 100, 100).translate(x, y) # 45 \
when 500
line(50, 50, 100, 100).translate(x, y) # 45 \
line(50, 100, 100, 100).translate(x, y) # 1579 _
when 600
line(100, 50, 100, 100).translate(x, y) # 6789 |
when 700
line(100, 50, 100, 100).translate(x, y) # 6789 |
line(50, 100, 100, 100).translate(x, y) # 1579 _
when 800
line(100, 50, 100, 100).translate(x, y) # 6789 |
line(50, 50, 100, 50).translate(x, y) # 289 -
when 900
line(100, 50, 100, 100).translate(x, y) # 6789 |
line(50, 100, 100, 100).translate(x, y) # 1579 _
line(50, 50, 100, 50).translate(x, y) # 289 -
when 1000
line(50, 100, 0, 100).translate(x, y) # 1579 _
when 2000
line(50, 50, 0, 50).translate(x, y) # 289 -
when 3000
line(50, 100, 0, 50).translate(x, y) # 3 \
when 4000
line(50, 50, 0, 100).translate(x, y) # 45 /
when 5000
line(50, 50, 0, 100).translate(x, y) # 45 /
line(50, 100, 0, 100).translate(x, y) # 1579 _
when 6000
line(0, 50, 0, 100).translate(x, y) # 6789 |
when 7000
line(50, 100, 0, 100).translate(x, y) # 1579 _
line(0, 50, 0, 100).translate(x, y) # 6789 |
when 8000
line(0, 50, 0, 100).translate(x, y) # 6789 |
line(50, 50, 0, 50).translate(x, y) # 289 -
when 9000
line(0, 50, 0, 100).translate(x, y) # 6789 |
line(50, 100, 0, 100).translate(x, y) # 1579 _
line(50, 50, 0, 50).translate(x, y) # 289 -
end
end
end
sequence = cister(message)
inject_evil_symbols(sequence) until perfect_square?(sequence.size)
path = 'tmp/inline.svg'
y = 0
img = Rasem::SVGImage.new(width: 100 * sequence.size, height: 100) do
sequence.map { |s| digits(s) }
.each_slice(Math.sqrt(sequence.size)) do |slice|
y += SPACE + 100
slice.each_with_index { |s, i| symbol(s, i, y) }
end
end
File.open(path, 'w') do |f|
img.write(f)
end