-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.rb
executable file
·89 lines (72 loc) · 1.38 KB
/
classes.rb
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
class Integer
def opposite
(self + 3) % 6
end
def adjacent
[-2, -1, 1, 2].map {|o| (self + o) % 6} .sort
end
def adjacent_clockwise
even? ? adjacent.sort.reverse : adjacent.sort
end
def edges
adjacent.map {|a| [a, self].sort}
end
def edges_clockwise
adjacent_clockwise.map {|a| [a, self].sort}
end
def corners
4.times.map {|i| [self, adjacent[i], adjacent[(i + 1) % 4]].sort}
end
def corners_clockwise
4.times.map {|i| [self, adjacent_clockwise[i], adjacent_clockwise[(i + 1) % 4]].sort}
end
def pieces
[*edges, *corners]
end
def pieces_clockwise
[edges_clockwise, corners_clockwise].transpose.flatten 1
end
def to_color
C[self]
end
def cube_equals other
self == -1 || other == -1 ? true : self == other
end
def dh6
[self % 6, (self / 6).floor]
end
end
class NilClass
def to_color
'?'
end
def to_str
''
end
end
class Array
def to_cube
Cube.new self
end
def ternary_eval
index = 0
index += 2 until (index >= length - 1) || yield(self[index])
return self[index + 1] unless index == length - 1
last
end
def h6
self[0] + self[1]*6
end
end
# puts ['true', 0, 'true', 1, 2].ternary_eval {|i| i == 'true'}
class String
def recurrence_time
c = Cube.new
i = 0
begin
i += 1
c.permutate! self
end until c.solved?
i
end
end