1
+ def gen_scramble len = 20
2
+ compress ( ( len * 2 ) . times . map { Moves . sample } . join ' ' ) . split [ 0 ...len ] . join ' '
3
+ end
4
+
5
+ def compress_sub algorithm
6
+ final = [ ]
7
+ algorithm . scan ( /[UD2' ]+|[RL2' ]+|[FB2' ]+/ ) . each do |group |
8
+ counts = { }
9
+ group . split . each do |move |
10
+ occurrences = case move [ 1 ..-1 ]
11
+ when '' then 1
12
+ when '2' then 2
13
+ when "'" then 3
14
+ end
15
+ if counts . include? ( move [ 0 ] )
16
+ counts [ move [ 0 ] ] += occurrences
17
+ else
18
+ counts [ move [ 0 ] ] = occurrences
19
+ end
20
+ end
21
+ counts . sort . each do |key , count |
22
+ count %= 4
23
+ next if count == 0
24
+ final << key + ( count == 1 ? '' : count == 2 ? '2' : "'" )
25
+ end
26
+ end
27
+ final . join ' '
28
+ end
29
+
30
+ def compress algorithm
31
+ while true
32
+ saved = algorithm
33
+ algorithm = compress_sub algorithm
34
+ return algorithm if saved == algorithm
35
+ end
36
+ end
37
+
38
+ def reverse algorithm
39
+ algorithm . split . reverse . map { |i | i . length == 1 ? i + "'" : i [ 1 ] == "'" ? i [ 0 ] : i } . join ' '
40
+ end
41
+
42
+ def _c a # only for face turns
43
+ compress ( a ) . gsub ( /[A-Z]'/ ) { |i | i [ 0 ] . downcase } . gsub ( /[A-Z]2/ ) { |i | ( i [ 0 ] . ord + 1 ) . chr } . gsub ' ' , ''
44
+ end
45
+
46
+ def _d a # only for face turns
47
+ a . split ( '' ) . join ( ' ' ) . gsub ( /[a-z]/ ) { |i | i . upcase + "'" } . gsub ( /[SMVEGC]/ ) { |i | ( i . ord - 1 ) . chr + '2' }
48
+ end
49
+
50
+ def _ct t # compress ternary ( [k, v] ) containing only face turns
51
+ t0 = t [ 0 ] . map { |i | i . join } . join ','
52
+ # >
53
+ t1 = ''
54
+ ls = false
55
+ t [ 1 ] . each do |i |
56
+ if i . is_a? Array
57
+ t1 << i . map { |i | i . join } . join ( ',' )
58
+ ls = false
59
+ elsif i . is_a? String
60
+ if ls
61
+ t1 << ',' + _c ( i )
62
+ else
63
+ t1 << ( i == '' ? '_' : _c ( i ) )
64
+ end
65
+ ls = true
66
+ else
67
+ raise "unexpected class (#{ i . class } ) in ternary expression"
68
+ end
69
+ end
70
+ ( t0 + '>' + t1 ) . gsub /(\d \d ),\1 / , '\1k'
71
+ end
72
+
73
+ def _dt t # decompress ternary ( [k, v] ) containing only face turns
74
+ t0 , t1 = *t . gsub ( /(\d \d )k/ , '\1,\1' ) . split ( '>' )
75
+ t0 = t0 . split ( ',' ) . map { |i | i . split ( '' ) . map &:to_i }
76
+ t1a = [ ]
77
+ t1 = t1 . split ( /(?=[a-zA-Z_])(?<=\d )|(?=\d )(?<=[a-zA-Z_])|,/ ) . map do |i |
78
+ if i =~ /[a-zA-Z_]/
79
+ i == '_' ? '' : _d ( i )
80
+ elsif i =~ /\d /
81
+ i . split ( '' ) . map &:to_i
82
+ else
83
+ raise "unexpected character found in #{ i } in ternary expression"
84
+ end
85
+ end
86
+ la = false
87
+ t1 . each do |i |
88
+ if i . is_a? String
89
+ t1a << i
90
+ la = false
91
+ elsif i . is_a? Array
92
+ if la
93
+ t1a [ -1 ] << i
94
+ else
95
+ t1a << [ i ]
96
+ end
97
+ la = true
98
+ end
99
+ end
100
+ [ t0 , t1a ]
101
+ end
102
+
103
+ def _ci i
104
+ if i [ 0 ] == :default_piece
105
+ 'd' + i [ 1 ] . map { |j | j . join } . join ( ',' )
106
+ elsif i [ 0 ] == :select
107
+ 's' + i [ 1 ] . join
108
+ else
109
+ raise 'error _ci'
110
+ end
111
+ end
112
+
113
+ def _di i
114
+ if i [ 0 ] == 'd'
115
+ [ :default_piece , i [ 1 ..-1 ] . split ( ',' ) . map { |j | j . split ( '' ) . map ( &:to_i ) } ]
116
+ elsif i [ 0 ] == 's'
117
+ [ :select , i [ 1 ..-1 ] . split ( '' ) . map ( &:to_i ) ]
118
+ else
119
+ raise 'error _di'
120
+ end
121
+ end
122
+
123
+ def _ca h
124
+ # h.to_a.map {|i| i[0].is_a?(Array) ? _ct(i) : i} # FOR SEPARATE STRINGS
125
+ h = h . to_a # see below
126
+ h0 = h . select { |i | i [ 0 ] . is_a? Symbol } . map { |i | _ci i } . join ( '|' )
127
+ h1 = h . select { |i | i [ 0 ] . is_a? Array } . map { |i | _ct i } . join ( '|' ) # for single long string
128
+ # h.map {|i| i.is_a?(String) ? i.scan(/.{1,81}/).join("\n") : i}
129
+ h0 + '!' + h1
130
+ end
131
+
132
+ def _da h
133
+ # Hash[h.map {|i| i.is_a?(String) ? _dt(i) : i}] # FOR SEPARATE STRINGS
134
+ i , h = *h . split ( '!' )
135
+ Hash [ i . split ( '|' ) . map { |j | _di j } ] . merge Hash [ h . split ( '|' ) . map { |i | _dt i } ] # for single long string
136
+ end
137
+
138
+ def _cA a
139
+ a . to_a . map { |i | i [ 0 ] . to_s + '%' + _ca ( i [ 1 ] ) } . join '$'
140
+ end
141
+
142
+ def _dA a
143
+ na = { }
144
+ a . split ( '$' ) . map do |i |
145
+ n , i = *i . split ( '%' )
146
+ na [ n . to_sym ] = _da ( i )
147
+ end
148
+ na
149
+ end
150
+
151
+ def writealgs
152
+ File . open ( 'algfile.txt' , 'w+' ) do |f |
153
+ f . print _cA Algorithms
154
+ end
155
+ end
156
+ def readalgs
157
+ File . open ( 'algfile.txt' , 'r' ) do |f |
158
+ # _dA(f.gets).each do |k, v|
159
+ # Algorithms[k] = v
160
+ # end
161
+ Algorithms . replace _dA f . gets
162
+ end
163
+ end
164
+
165
+ MvMap = {
166
+ "u" => "D Y" ,
167
+ "d" => "U Y'" ,
168
+ "r" => "L X" ,
169
+ "l" => "R X'" ,
170
+ "f" => "B Z" ,
171
+ "b" => "F Z'" ,
172
+ "M" => "R L' X'" ,
173
+ "E" => "U D' Y'" ,
174
+ "S" => "F' B Z"
175
+ }
176
+
177
+ Rotations = {
178
+ "Y" => [ 0 , 4 ] ,
179
+ "Y'" => [ 0 , 1 ] ,
180
+ "Y2" => [ 0 , 2 ] ,
181
+ "X" => [ 2 , 0 ] ,
182
+ "X'" => [ 5 , 3 ] ,
183
+ "X2" => [ 3 , 2 ] ,
184
+ "Z" => [ 4 , 5 ] ,
185
+ "Z'" => [ 1 , 5 ] ,
186
+ "Z2" => [ 3 , 5 ]
187
+ }
188
+
189
+ def interpret algorithm
190
+ # puts algorithm
191
+ algorithm . gsub! ( /([udrlfbMES]['2]?)/ ) do |i |
192
+ move , mod = i [ 0 ] , i [ 1 ..1 ]
193
+ MvMap [ move ] . split . map { |i | ( i + mod ) . gsub ( "''" , '' ) . gsub "'2" , '2' } . join ' '
194
+ end
195
+ # puts algorithm
196
+ new_alg = [ ]
197
+ current = [ 0 , 5 ]
198
+ algorithm . split . each do |i |
199
+ if i =~ /[XYZ]/
200
+ current = transpose_position Rotations [ i ] , [ 0 , 5 ] , current
201
+ else
202
+ new_alg << transpose_moves ( i , [ 0 , 5 ] , current )
203
+ end
204
+ end
205
+ compress new_alg . join ' '
206
+ end
0 commit comments