-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
115 lines (96 loc) · 4.45 KB
/
solver.py
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
import pycuber as pc
import string
class Solver():
def __init__(self):
self.cube = pc.Cube()
self.ALPHABETX = string.ascii_uppercase[:24]
self.COLOR2FACE = {
"red": 'B',
"yellow": 'U',
"green": 'L',
"white": 'D',
"orange" : 'F',
"blue": 'R'
}
__EDGE_FF__ = 'UBURUFULFUFRFDFLRURBRDRFBUBLBDBRLULFLDLBDFDRDBDL'
self.EDGE_FF = [__EDGE_FF__[i:i+2] for i in range(0, len(__EDGE_FF__), 2)]
self.EDGE_FF_SET = set(frozenset(list(i)) for i in self.EDGE_FF)
self.EDGE_MAP = {i[0]:i[1] for i in zip(self.ALPHABETX, self.EDGE_FF)}
__e_ff_solved__ = set([frozenset(list('RU'))])
self.e_available_slots_ff = self.EDGE_FF_SET - __e_ff_solved__
__CORNER_FF__ = 'UBLUBRUFRUFLFLUFRUFDRFDLRFURBURBDRDFBRUBLUBDLBDRLBULFULDFLBDDFLDFRDBRDBL'
self.CORNER_FF = [__CORNER_FF__[i:i+3] for i in range(0, len(__CORNER_FF__), 3)]
self.CORNER_FF_SET = set(frozenset(i) for i in self.CORNER_FF)
self.CORNER_MAP = {i[0]:i[1] for i in zip(self.ALPHABETX, self.CORNER_FF)}
__c_ff_solved__ = set([frozenset(list('LUB'))])
self.c_available_slots_ff = self.CORNER_FF_SET - __c_ff_solved__
def scramble(self, algo):
self.cube(algo)
def e_slot2seq(self, slot):
slot_ff = self.EDGE_MAP[slot]
piece_cc = self.cube.__getitem__(slot_ff)
piece_ff = self.COLOR2FACE[piece_cc[slot_ff[0]].colour] + self.COLOR2FACE[piece_cc[slot_ff[1]].colour]
_slot_ff = piece_ff
seq = self.ALPHABETX[self.EDGE_FF.index(_slot_ff)]
return seq
def e_getEmptySlot(self):
asff = next(iter(self.e_available_slots_ff))
tmp = ''.join(asff)
return self.ALPHABETX[self.EDGE_FF.index(tmp)]
def get_e_sequence(self, input_seq='B'):
sequence = ''
while len(self.e_available_slots_ff) > 0:
seq = self.e_slot2seq(input_seq)
if seq in ['B', 'I']:
sequence += '-'
seq = self.e_getEmptySlot() #find next slot
elif frozenset(list(self.EDGE_MAP[seq])) not in self.e_available_slots_ff:
seq = self.e_getEmptySlot()
self.e_available_slots_ff.remove(frozenset(list(self.EDGE_MAP[seq])))
elif seq == input_seq:
self.e_available_slots_ff.remove(frozenset(list(self.EDGE_MAP[seq])))
seq = self.e_getEmptySlot()
else:
self.e_available_slots_ff.remove(frozenset(list(self.EDGE_MAP[seq])))
sequence += seq
input_seq = seq
return sequence
def c_slot2seq(self, slot):
slot_ff = self.CORNER_MAP[slot]
piece_cc = self.cube.__getitem__(slot_ff)
piece_ff = ''.join(sorted(self.COLOR2FACE[piece_cc[slot_ff[0]].colour] + self.COLOR2FACE[piece_cc[slot_ff[1]].colour] + self.COLOR2FACE[piece_cc[slot_ff[2]].colour]))
_slot_ff = piece_ff
seq = self.ALPHABETX[self.CORNER_FF.index(_slot_ff)]
return seq
def c_getEmptySlot(self):
asff = next(iter(self.c_available_slots_ff))
tmp = ''.join(sorted(asff))
return self.ALPHABETX[self.CORNER_FF.index(tmp)]
def get_c_sequence(self, input_seq='A'):
sequence = ''
while len(self.c_available_slots_ff) > 0:
seq = self.c_slot2seq(input_seq)
if seq in ['A', 'N', 'Q']:
sequence += '-'
seq = self.c_getEmptySlot() #find next slot
elif frozenset(list(self.CORNER_MAP[seq])) not in self.c_available_slots_ff:
seq = self.c_getEmptySlot()
self.c_available_slots_ff.remove(frozenset(list(self.CORNER_MAP[seq])))
elif seq == input_seq:
self.c_available_slots_ff.remove(frozenset(list(self.CORNER_MAP[seq])))
seq = self.c_getEmptySlot()
else:
self.c_available_slots_ff.remove(frozenset(list(self.CORNER_MAP[seq])))
sequence += seq
input_seq = seq
return sequence
def get_parity():
e_sequence = self.get_e_sequence()
if __name__ == '__main__':
SCRAMBLE = "D' L' R' U D B2 U' D2 B2 L' F' L' D' R F2 B2 L2 D F D B R' U2 B L2"
solver = Solver()
solver.scramble(SCRAMBLE)
e, c = solver.get_e_sequence(), solver.get_c_sequence()
print('SCRAMBLE', SCRAMBLE)
print('EDGE SEQ:', e)
print('CORNER SEQ:', c)