 # Pyraminx short scrambles

Oct 13th, 2021
992
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sys
2. import collections
3. import copy
4.
5. from db import db
6.
7. # Sides
8. F = 0
9. L = 1
10. R = 2
11. D = 3
12.
13. # Moves
14. MOVES = {
15.   'U': [[[F, 0], [L, 0], [R, 0]],
16.         [[F, 1], [L, 1], [R, 1]],
17.         [[F, 2], [L, 2], [R, 2]],
18.         [[F, 3], [L, 3], [R, 3]]],
19.   'L': [[[F, 4], [D, 0], [L, 8]],
20.         [[F, 1], [D, 2], [L, 6]],
21.         [[F, 5], [D, 1], [L, 7]],
22.         [[F, 6], [D, 5], [L, 3]]],
23.   'R': [[[R, 4], [D, 4], [F, 8]],
24.         [[R, 1], [D, 7], [F, 6]],
25.         [[R, 5], [D, 3], [F, 7]],
26.         [[R, 6], [D, 2], [F, 3]]],
27.   'B': [[[L, 4], [D, 8], [R, 8]],
28.         [[L, 1], [D, 5], [R, 6]],
29.         [[L, 5], [D, 6], [R, 7]],
30.         [[L, 6], [D, 7], [R, 3]]],
31. }
32.
33. SOLVED = [[F] * 9, [L] * 9, [R] * 9, [D] * 9]
34.
35. ALL_MOVES = list(MOVES.keys()) + [x + '\'' for x in MOVES.keys()]
36.
37. def applyMove(position, move):
38.   subMove = move
39.   transformations = None
40.   if subMove in MOVES:
41.     transformations = MOVES[subMove]
42.   elif subMove.upper() in MOVES:
43.     return
44.     #transformations = MOVES[subMove.upper()]
45.   if not transformations:
46.     print('ERROR: Unknown move ' + move)
47.     return
48.   if '\'' in move:
49.     transformations = [list(reversed(x)) for x in transformations]
50.
51.   for transformation in transformations:
52.     saved = position[transformation[-1]][transformation[-1]]
53.     for i in reversed(range(len(transformation) - 1)):
54.       from_ = transformation[i]
55.       to_ = transformation[i + 1]
56.       position[to_][to_] = position[from_][from_]
57.     position[transformation][transformation] = saved
58.
59. def executeScramble(moves):
60.   position = copy.deepcopy(SOLVED)
61.   for move in moves:
62.     applyMove(position, move)
63.   return position
64.
65. def toStr(position):
66.   return ''.join([''.join([str(y) for y in x]) for x in position])
67.
68. def positionsWithMaxDepth(num):
69.   scrambles = [[]]
70.   for i in range(num):
71.     for scramble in scrambles:
72.       if len(scramble) != i:
73.         continue
74.       for move in ALL_MOVES:
75.         if scramble and scramble[-1] == move:
76.           continue
77.         scrambles += [scramble + [move]]
78.
79.   out = {}
80.   for scramble in scrambles:
81.     position = toStr(executeScramble(scramble))
82.     if position not in out:
83.       out[position] = len(scramble)
84.   return out
85.
86.
87. positions = positionsWithMaxDepth(6)
88.
89. cursor = db.cursor()
90. cursor.execute("SELECT competitionId, roundTypeId, groupId, isExtra, scrambleNum, scramble FROM Scrambles WHERE eventId='pyram'")
91.
92. tipCounts = collections.defaultdict(lambda: 0)
93.
94. for row in cursor.fetchall():
95.   scr = row.split(' ')
96.   numTips = len([x for x in scr if x in ('u', 'l', 'r', 'b')])
97.   pos = toStr(executeScramble(scr))
98.   if pos in positions:
99.     if positions[pos] + numTips == 6:
100.       tipCounts[numTips] += 1
101.
102.     if positions[pos] < 3:
103.       print(row)
104.
105. print(tipCounts)
106.
RAW Paste Data