Skip to content

Commit 707d0f0

Browse files
author
gorilskij
committed
switched to RubyMine
0 parents  commit 707d0f0

9 files changed

+1409
-0
lines changed

algorithm_data.rb

+538
Large diffs are not rendered by default.

algorithm_manipulation.rb

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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

classes.rb

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
class Integer
2+
def opposite
3+
(self + 3) % 6
4+
end
5+
6+
def adjacent
7+
[-2, -1, 1, 2].map {|o| (self + o) % 6} .sort
8+
end
9+
10+
def adjacent_clockwise
11+
even? ? adjacent.sort.reverse : adjacent.sort
12+
end
13+
14+
def edges
15+
adjacent.map {|a| [a, self].sort}
16+
end
17+
18+
def edges_clockwise
19+
adjacent_clockwise.map {|a| [a, self].sort}
20+
end
21+
22+
def corners
23+
4.times.map {|i| [self, adjacent[i], adjacent[(i + 1) % 4]].sort}
24+
end
25+
26+
def corners_clockwise
27+
4.times.map {|i| [self, adjacent_clockwise[i], adjacent_clockwise[(i + 1) % 4]].sort}
28+
end
29+
30+
def pieces
31+
[*edges, *corners]
32+
end
33+
34+
def pieces_clockwise
35+
[edges_clockwise, corners_clockwise].transpose.flatten 1
36+
end
37+
38+
def to_color
39+
C[self]
40+
end
41+
42+
def cube_equals other
43+
self == -1 || other == -1 ? true : self == other
44+
end
45+
46+
def dh6
47+
[self % 6, (self / 6).floor]
48+
end
49+
end
50+
51+
class NilClass
52+
def to_color
53+
'?'
54+
end
55+
def to_str
56+
''
57+
end
58+
end
59+
60+
class Array
61+
def to_cube
62+
Cube.new self
63+
end
64+
65+
def ternary_eval
66+
index = 0
67+
index += 2 until (index >= length - 1) || yield(self[index])
68+
return self[index + 1] unless index == length - 1
69+
last
70+
end
71+
72+
def h6
73+
self[0] + self[1]*6
74+
end
75+
end
76+
77+
# puts ['true', 0, 'true', 1, 2].ternary_eval {|i| i == 'true'}
78+
79+
class String
80+
def recurrence_time
81+
c = Cube.new
82+
i = 0
83+
begin
84+
i += 1
85+
c.permutate! self
86+
end until c.solved?
87+
i
88+
end
89+
end

color_printer.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
ANSIcolors = {
2+
"R" => 101,
3+
"G" => 102,
4+
"Y" => 103,
5+
"B" => 104,
6+
"O" => 105,
7+
"W" => 107
8+
}
9+
10+
class String
11+
def tint
12+
"\e[#{ANSIcolors[self]}m \e[0m"
13+
end
14+
end
15+
16+
class Cube
17+
def in_color
18+
canvas = ([nil]*6).map {[nil]*9}
19+
canvas.each_with_index do |f, i|
20+
canvas[i][4] = i
21+
@pieces.each do |c|
22+
[*i.edges, *i.corners].each do |e|
23+
if c[1].sort == e
24+
canvas[i][P[i][(c[1] - [i]).sort]] = c[0][c[1].index i]
25+
end
26+
end
27+
end
28+
end
29+
cs = ''
30+
[0, 3, 6].each do |i|
31+
cs += ' '*14 + canvas[3][i..i+2].map(&:to_color).map(&:tint).join(' ') + "\n\n"
32+
end
33+
cs += "\n"
34+
[0, 3, 6].each do |i|
35+
[1, 5, 4, 2].each do |f|
36+
cs += canvas[f][i..i+2].map(&:to_color).map(&:tint).join(' ') + ' '
37+
end
38+
cs += "\n\n"
39+
end
40+
cs += "\n"
41+
[0, 3, 6].each do |i|
42+
cs += ' '*14 + canvas[0][i..i+2].map(&:to_color).map(&:tint).join(' ') + "\n\n"
43+
end
44+
"\n" + cs + "\n"
45+
end
46+
end

0 commit comments

Comments
 (0)