passagemath-rubiks 10.6.46__cp310-cp310-macosx_13_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of passagemath-rubiks might be problematic. Click here for more details.
- passagemath_rubiks/__init__.py +3 -0
- passagemath_rubiks-10.6.46.dist-info/METADATA +157 -0
- passagemath_rubiks-10.6.46.dist-info/RECORD +17 -0
- passagemath_rubiks-10.6.46.dist-info/WHEEL +6 -0
- passagemath_rubiks-10.6.46.dist-info/top_level.txt +3 -0
- sage/all__sagemath_rubiks.py +4 -0
- sage/interfaces/all__sagemath_rubiks.py +8 -0
- sage/interfaces/rubik.py +341 -0
- sage/libs/all__sagemath_rubiks.py +1 -0
- sage/libs/rubik.cpython-310-darwin.so +0 -0
- sage/libs/rubik.pyx +1 -0
- sage_wheels/bin/cu2 +0 -0
- sage_wheels/bin/cubex +0 -0
- sage_wheels/bin/dikcube +0 -0
- sage_wheels/bin/mcube +0 -0
- sage_wheels/bin/optimal +0 -0
- sage_wheels/bin/size222 +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: passagemath-rubiks
|
|
3
|
+
Version: 10.6.46
|
|
4
|
+
Summary: passagemath: Algorithms for Rubik's cube
|
|
5
|
+
Author-email: The Sage Developers <sage-support@googlegroups.com>
|
|
6
|
+
Maintainer: Matthias Köppe, passagemath contributors
|
|
7
|
+
License-Expression: GPL-2.0-or-later
|
|
8
|
+
Project-URL: release notes, https://github.com/passagemath/passagemath/releases
|
|
9
|
+
Project-URL: repo (upstream), https://github.com/sagemath/sage
|
|
10
|
+
Project-URL: repo, https://github.com/passagemath/passagemath
|
|
11
|
+
Project-URL: documentation, https://passagemath.org/docs/latest
|
|
12
|
+
Project-URL: homepage (upstream), https://www.sagemath.org
|
|
13
|
+
Project-URL: discourse, https://passagemath.discourse.group
|
|
14
|
+
Project-URL: tracker (upstream), https://github.com/sagemath/sage/issues
|
|
15
|
+
Project-URL: tracker, https://github.com/passagemath/passagemath/issues
|
|
16
|
+
Classifier: Development Status :: 6 - Mature
|
|
17
|
+
Classifier: Intended Audience :: Education
|
|
18
|
+
Classifier: Intended Audience :: Science/Research
|
|
19
|
+
Classifier: Operating System :: POSIX
|
|
20
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
21
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
28
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
29
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
30
|
+
Requires-Python: <3.15,>=3.10
|
|
31
|
+
Description-Content-Type: text/x-rst
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
Requires-Dist: passagemath-groups; extra == "test"
|
|
34
|
+
Requires-Dist: passagemath-repl; extra == "test"
|
|
35
|
+
|
|
36
|
+
===============================================================================
|
|
37
|
+
passagemath: Algorithms for Rubik's cube
|
|
38
|
+
===============================================================================
|
|
39
|
+
|
|
40
|
+
`passagemath <https://github.com/passagemath/passagemath>`__ is open
|
|
41
|
+
source mathematical software in Python, released under the GNU General
|
|
42
|
+
Public Licence GPLv2+.
|
|
43
|
+
|
|
44
|
+
It is a fork of `SageMath <https://www.sagemath.org/>`__, which has been
|
|
45
|
+
developed 2005-2025 under the motto “Creating a Viable Open Source
|
|
46
|
+
Alternative to Magma, Maple, Mathematica, and MATLAB”.
|
|
47
|
+
|
|
48
|
+
The passagemath fork uses the motto "Creating a Free Passage Between the
|
|
49
|
+
Scientific Python Ecosystem and Mathematical Software Communities."
|
|
50
|
+
It was created in October 2024 with the following goals:
|
|
51
|
+
|
|
52
|
+
- providing modularized installation with pip,
|
|
53
|
+
- establishing first-class membership in the scientific Python
|
|
54
|
+
ecosystem,
|
|
55
|
+
- giving `clear attribution of upstream
|
|
56
|
+
projects <https://groups.google.com/g/sage-devel/c/6HO1HEtL1Fs/m/G002rPGpAAAJ>`__,
|
|
57
|
+
- providing independently usable Python interfaces to upstream
|
|
58
|
+
libraries,
|
|
59
|
+
- offering `platform portability and integration testing
|
|
60
|
+
services <https://github.com/passagemath/passagemath/issues/704>`__
|
|
61
|
+
to upstream projects,
|
|
62
|
+
- inviting collaborations with upstream projects,
|
|
63
|
+
- `building a professional, respectful, inclusive
|
|
64
|
+
community <https://groups.google.com/g/sage-devel/c/xBzaINHWwUQ>`__,
|
|
65
|
+
- `empowering Sage users to participate in the scientific Python ecosystem
|
|
66
|
+
<https://github.com/passagemath/passagemath/issues/248>`__ by publishing packages,
|
|
67
|
+
- developing a port to `Pyodide <https://pyodide.org/en/stable/>`__ for
|
|
68
|
+
serverless deployment with Javascript,
|
|
69
|
+
- developing a native Windows port.
|
|
70
|
+
|
|
71
|
+
`Full documentation <https://passagemath.org/docs/latest/html/en/index.html>`__ is
|
|
72
|
+
available online.
|
|
73
|
+
|
|
74
|
+
passagemath attempts to support and provides binary wheels suitable for
|
|
75
|
+
all major Linux distributions and recent versions of macOS.
|
|
76
|
+
|
|
77
|
+
Binary wheels for native Windows (x86_64) are are available for a subset of
|
|
78
|
+
the passagemath distributions. Use of the full functionality of passagemath
|
|
79
|
+
on Windows currently requires the use of Windows Subsystem for Linux (WSL)
|
|
80
|
+
or virtualization.
|
|
81
|
+
|
|
82
|
+
The supported Python versions in the passagemath 10.6.x series are 3.10.x-3.14.x.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
About this pip-installable distribution package
|
|
86
|
+
-----------------------------------------------
|
|
87
|
+
|
|
88
|
+
This pip-installable distribution ``passagemath-rubiks`` provides an interface
|
|
89
|
+
to several programs for working with Rubik's cubes.
|
|
90
|
+
|
|
91
|
+
Michael Reid (GPL) http://www.cflmath.com/~reid/Rubik/optimal_solver.html
|
|
92
|
+
|
|
93
|
+
- optimal - uses many pre-computed tables to find an optimal
|
|
94
|
+
solution to the 3x3x3 Rubik's cube
|
|
95
|
+
|
|
96
|
+
Dik T. Winter (MIT License)
|
|
97
|
+
|
|
98
|
+
- cube - uses Kociemba's algorithm to iteratively find a short
|
|
99
|
+
solution to the 3x3x3 Rubik's cube
|
|
100
|
+
- size222 - solves a 2x2x2 Rubik's cube
|
|
101
|
+
|
|
102
|
+
Eric Dietz (GPL) https://web.archive.org/web/20121212175710/http://www.wrongway.org/?rubiksource
|
|
103
|
+
|
|
104
|
+
- cu2 - A fast, non-optimal 2x2x2 solver
|
|
105
|
+
- cubex - A fast, non-optimal 3x3x3 solver
|
|
106
|
+
- mcube - A fast, non-optimal 4x4x4 solver
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
What is included
|
|
110
|
+
----------------
|
|
111
|
+
|
|
112
|
+
* `Interface <https://passagemath.org/docs/latest/html/en/reference/interfaces/sage/interfaces/rubik.html#module-sage.interfaces.rubik>`_
|
|
113
|
+
|
|
114
|
+
* `Features <https://passagemath.org/docs/latest/html/en/reference/spkg/sage/features/rubiks.html#module-sage.features.rubiks>`_ (via passagemath-environment)
|
|
115
|
+
|
|
116
|
+
* Binary wheels on PyPI contain prebuilt copies of rubiks executables.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
Examples
|
|
120
|
+
--------
|
|
121
|
+
|
|
122
|
+
Using rubiks programs on the command line::
|
|
123
|
+
|
|
124
|
+
$ pipx run --pip-args="--prefer-binary" --spec "passagemath-rubiks" sage -sh -c cubex
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
Finding the installation location of a rubiks program::
|
|
128
|
+
|
|
129
|
+
$ pipx run --pip-args="--prefer-binary" --spec "passagemath-rubiks[test]" ipython
|
|
130
|
+
|
|
131
|
+
In [1]: from sage.features.rubiks import cubex
|
|
132
|
+
|
|
133
|
+
In [2]: cubex().absolute_filename()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
Using the Python interface::
|
|
137
|
+
|
|
138
|
+
$ pipx run --pip-args="--prefer-binary" --spec "passagemath-rubiks[test]" ipython
|
|
139
|
+
|
|
140
|
+
In [1]: from sage.interfaces.rubik import *
|
|
141
|
+
|
|
142
|
+
In [2]: C = RubiksCube("R U F L B D")
|
|
143
|
+
|
|
144
|
+
In [3]: sol = CubexSolver().solve(C.facets()); sol
|
|
145
|
+
Out[3]: "U' L' L' U L U' L U D L L D' L' D L' D' L D L' U' L D' L' U L' B' U' L' U B L D L D' U' L' U L B L B' L' U L U' L' F' L' F L' F L F' L' D' L' D D L D' B L B' L B' L B F' L F F B' L F' B D' D' L D B' B' L' D' B U' U' L' B' D' F' F' L D F'"
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
Using sage.groups.perm_gps::
|
|
149
|
+
|
|
150
|
+
$ pipx run --pip-args="--prefer-binary" --spec "passagemath-rubiks[test]" ipython
|
|
151
|
+
|
|
152
|
+
In [1]: from passagemath_rubiks import *
|
|
153
|
+
|
|
154
|
+
In [2]: rubik = CubeGroup(); state = rubik.faces("R")
|
|
155
|
+
|
|
156
|
+
In [3]: rubik.solve(state)
|
|
157
|
+
Out[3]: 'R'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
passagemath_rubiks-10.6.46.dist-info/RECORD,,
|
|
2
|
+
passagemath_rubiks-10.6.46.dist-info/WHEEL,sha256=NiDEgsEgCo_FQMmjOAqM82taU_S298AgeyCKlTR6Sr8,153
|
|
3
|
+
passagemath_rubiks-10.6.46.dist-info/top_level.txt,sha256=FMgD4ZM3GVwb20o5Ad_LYdWrdFM10nZnrN71URGfQGs,25
|
|
4
|
+
passagemath_rubiks-10.6.46.dist-info/METADATA,sha256=1VPOyNmd-tO2-slCa8mnSYtQ5z-rbczn5xXLR1nU9eM,6408
|
|
5
|
+
sage_wheels/bin/cubex,sha256=9QzXdnXBoUUq39uX-wN9zOuISyGJZtM3Hkij2ARDSCQ,249448
|
|
6
|
+
sage_wheels/bin/cu2,sha256=eUM51DdbCJCUj3T5DAESek2fyEcJ71xpBm5jfNdIeOY,184144
|
|
7
|
+
sage_wheels/bin/mcube,sha256=lTlhFR42H1D04r3obP92iGVmZWAkL0o5iNIh7uxxwSI,289800
|
|
8
|
+
sage_wheels/bin/optimal,sha256=5uKTXneREsfLZrNCBDLWUb8Y-ihEolmTEleM5mSo2Uk,104232
|
|
9
|
+
sage_wheels/bin/dikcube,sha256=slwcJDQTgFEY04r1sUFyiaekY9QLPK8spoDu_cPJBow,69208
|
|
10
|
+
sage_wheels/bin/size222,sha256=7mUPVlbHJqollcOziA0H8mPAaCawUJIeeGyOXyVcxFA,50856
|
|
11
|
+
passagemath_rubiks/__init__.py,sha256=s6SS1hH2ZcoBAPlMHrjl6elW0Rw8CBEGeMubfQUNcPI,86
|
|
12
|
+
sage/all__sagemath_rubiks.py,sha256=7mJ-YvWC9PY9BQD0fF_sUu1YDlEceafc2oHrl-uKLK0,106
|
|
13
|
+
sage/libs/all__sagemath_rubiks.py,sha256=NwzyxDmQ4ARkr80f2jCvoIx0PgxV4ST30myyuf6PIC8,45
|
|
14
|
+
sage/libs/rubik.pyx,sha256=NwzyxDmQ4ARkr80f2jCvoIx0PgxV4ST30myyuf6PIC8,45
|
|
15
|
+
sage/libs/rubik.cpython-310-darwin.so,sha256=6ZPeIBiwNALP1fH5W60VuNkbzOWaNiHDtxENG1XTGBM,53784
|
|
16
|
+
sage/interfaces/all__sagemath_rubiks.py,sha256=C8lJV2QqUEHE1a50uaQHU7lCVmgfCQ0Io4ZP9_S2J5o,169
|
|
17
|
+
sage/interfaces/rubik.py,sha256=V5T8iXOcrnYIhOCo_JPwmrF01IbpeipAoNWHcHHx6zQ,11794
|
sage/interfaces/rubik.py
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-rubiks
|
|
2
|
+
# sage.doctest: optional - rubiks
|
|
3
|
+
r"""
|
|
4
|
+
Interface to several Rubik's cube solvers.
|
|
5
|
+
|
|
6
|
+
The first is by Michael Reid, and tries to find an optimal solution given
|
|
7
|
+
the cube's state, and may take a long time.
|
|
8
|
+
See http://www.math.ucf.edu/~reid/Rubik/optimal_solver.html
|
|
9
|
+
|
|
10
|
+
The second is by Eric Dietz, and uses a standard (?) algorithm to
|
|
11
|
+
solve the cube one level at a time. It is extremely fast, but often
|
|
12
|
+
returns a far from optimal solution.
|
|
13
|
+
See https://web.archive.org/web/20121212175710/http://www.wrongway.org/?rubiksource
|
|
14
|
+
|
|
15
|
+
The third is by Dik Winter and implements Kociemba's algorithm which
|
|
16
|
+
finds reasonable solutions relatively quickly, and if it is kept running
|
|
17
|
+
will eventually find the optimal solution.
|
|
18
|
+
|
|
19
|
+
AUTHOR:
|
|
20
|
+
|
|
21
|
+
-- Optimal was written by Michael Reid <reid@math.ucf.edu> (2004)
|
|
22
|
+
-- Cubex was written by Eric Dietz <root@wrongway.org> (2003)
|
|
23
|
+
-- Kociemba was written by Dik T. Winter <dik.winter@cwi.nl> (1993)
|
|
24
|
+
-- Initial interface by Robert Bradshaw (2007-08)
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
########################################################################
|
|
28
|
+
# Copyright (C) 2007 Robert Bradshaw <robertwb@math.washington.edu>
|
|
29
|
+
#
|
|
30
|
+
# Distributed under the terms of the GNU General Public License (GPL)
|
|
31
|
+
#
|
|
32
|
+
# The full text of the GPL is available at:
|
|
33
|
+
#
|
|
34
|
+
# https://www.gnu.org/licenses/
|
|
35
|
+
########################################################################
|
|
36
|
+
|
|
37
|
+
import pexpect
|
|
38
|
+
import time
|
|
39
|
+
import shlex
|
|
40
|
+
|
|
41
|
+
from . import quit
|
|
42
|
+
|
|
43
|
+
from sage.cpython.string import bytes_to_str
|
|
44
|
+
from sage.groups.perm_gps.cubegroup import index2singmaster
|
|
45
|
+
import sage.features.rubiks
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Can't seem to find consistency in letter ordering
|
|
49
|
+
# between us and them... These are copied from the source.
|
|
50
|
+
optimal_solver_tokens = ["UF", "UR", "UB", "UL",
|
|
51
|
+
"DF", "DR", "DB", "DL",
|
|
52
|
+
"FR", "FL", "BR", "BL",
|
|
53
|
+
"FU", "RU", "BU", "LU",
|
|
54
|
+
"FD", "RD", "BD", "LD",
|
|
55
|
+
"RF", "LF", "RB", "LB",
|
|
56
|
+
"UFR", "URB", "UBL", "ULF",
|
|
57
|
+
"DRF", "DFL", "DLB", "DBR",
|
|
58
|
+
"FRU", "RBU", "BLU", "LFU",
|
|
59
|
+
"RFD", "FLD", "LBD", "BRD",
|
|
60
|
+
"RUF", "BUR", "LUB", "FUL",
|
|
61
|
+
"FDR", "LDF", "BDL", "RDB"]
|
|
62
|
+
|
|
63
|
+
# The input format.
|
|
64
|
+
optimal_solver_format = "UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class SingNot:
|
|
68
|
+
"""
|
|
69
|
+
This class is to resolve difference between various Singmaster notation.
|
|
70
|
+
|
|
71
|
+
Case is ignored, and the second and third letters may be swapped.
|
|
72
|
+
|
|
73
|
+
EXAMPLES::
|
|
74
|
+
|
|
75
|
+
sage: from sage.interfaces.rubik import SingNot
|
|
76
|
+
sage: SingNot("acb") == SingNot("ACB")
|
|
77
|
+
True
|
|
78
|
+
sage: SingNot("acb") == SingNot("bca")
|
|
79
|
+
False
|
|
80
|
+
"""
|
|
81
|
+
def __init__(self, s):
|
|
82
|
+
self.rep = s
|
|
83
|
+
self.canonical = (s[0] + "".join(sorted(s[1:]))).lower()
|
|
84
|
+
|
|
85
|
+
def __eq__(self, other):
|
|
86
|
+
return isinstance(other, SingNot) and other.canonical == self.canonical
|
|
87
|
+
|
|
88
|
+
def __repr__(self):
|
|
89
|
+
return self.rep
|
|
90
|
+
|
|
91
|
+
def __hash__(self):
|
|
92
|
+
return hash(self.canonical)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# This is our list
|
|
96
|
+
singmaster_list = [''] + [SingNot(index2singmaster(i + 1)) for i in range(48)]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class OptimalSolver:
|
|
100
|
+
"""
|
|
101
|
+
Interface to Michael Reid's optimal Rubik's Cube solver.
|
|
102
|
+
"""
|
|
103
|
+
def __init__(self, verbose=False, wait=True):
|
|
104
|
+
self.verbose = verbose
|
|
105
|
+
self.start()
|
|
106
|
+
if wait:
|
|
107
|
+
print("Initializing tables...")
|
|
108
|
+
self.ready()
|
|
109
|
+
print("Done.")
|
|
110
|
+
|
|
111
|
+
def start(self):
|
|
112
|
+
cmd = shlex.quote(sage.features.rubiks.optimal().absolute_filename())
|
|
113
|
+
child = pexpect.spawn(cmd)
|
|
114
|
+
quit.register_spawned_process(child.pid, cmd)
|
|
115
|
+
child.timeout = None
|
|
116
|
+
self.child = child
|
|
117
|
+
self._ready = False
|
|
118
|
+
|
|
119
|
+
def stop(self):
|
|
120
|
+
if self.child:
|
|
121
|
+
self.child.sendline(chr(3)) # send ctrl-c
|
|
122
|
+
self.child.sendline(chr(4)) # send ctrl-d
|
|
123
|
+
self.child.close(True)
|
|
124
|
+
self.child = None
|
|
125
|
+
|
|
126
|
+
def ready(self):
|
|
127
|
+
if not self._ready:
|
|
128
|
+
self.child.expect('enter cube')
|
|
129
|
+
self._ready = True
|
|
130
|
+
|
|
131
|
+
def __call__(self, facets):
|
|
132
|
+
return self.solve(facets)
|
|
133
|
+
|
|
134
|
+
def solve(self, facets):
|
|
135
|
+
"""
|
|
136
|
+
The initial startup and precomputation are substantial...
|
|
137
|
+
|
|
138
|
+
.. TODO:: Let it keep searching once it found a solution?
|
|
139
|
+
|
|
140
|
+
EXAMPLES::
|
|
141
|
+
|
|
142
|
+
sage: # optional - rubiks
|
|
143
|
+
sage: from sage.interfaces.rubik import *
|
|
144
|
+
sage: solver = DikSolver()
|
|
145
|
+
sage: solver = OptimalSolver() # long time (28s on sage.math, 2012)
|
|
146
|
+
Initializing tables...
|
|
147
|
+
Done.
|
|
148
|
+
sage: C = RubiksCube("R U")
|
|
149
|
+
sage: solver.solve(C.facets())
|
|
150
|
+
'R U'
|
|
151
|
+
sage: C = RubiksCube("R U F L B D")
|
|
152
|
+
sage: solver.solve(C.facets())
|
|
153
|
+
'R U F L B D'
|
|
154
|
+
sage: C = RubiksCube("R2 D2")
|
|
155
|
+
sage: solver.solve(C.facets())
|
|
156
|
+
'R2 D2'
|
|
157
|
+
"""
|
|
158
|
+
self.ready()
|
|
159
|
+
self.child.sendline(self.format_cube(facets))
|
|
160
|
+
self.child.expect(r"([LRUDBF'2 ]+)\s+\((\d+)q\*?, (\d+)f\*?\)")
|
|
161
|
+
self.child.sendline(chr(3)) # send ctrl-c
|
|
162
|
+
return bytes_to_str(self.child.match.groups()[0]).strip()
|
|
163
|
+
|
|
164
|
+
def format_cube(self, facets):
|
|
165
|
+
L = []
|
|
166
|
+
optimal_solver_list = [SingNot(x) for x in optimal_solver_tokens]
|
|
167
|
+
for f in optimal_solver_format.split(" "):
|
|
168
|
+
ix = facets[singmaster_list.index(SingNot(f)) - 1]
|
|
169
|
+
facet = singmaster_list[ix]
|
|
170
|
+
L.append(optimal_solver_list[optimal_solver_list.index(facet)])
|
|
171
|
+
return " ".join(str(f) for f in L)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
move_map = {
|
|
175
|
+
"LD": "L'",
|
|
176
|
+
"LU": "L",
|
|
177
|
+
"RD": "R",
|
|
178
|
+
"RU": "R'",
|
|
179
|
+
"FA": "F",
|
|
180
|
+
"FC": "F'",
|
|
181
|
+
"BA": "B'",
|
|
182
|
+
"BC": "B",
|
|
183
|
+
"UR": "U",
|
|
184
|
+
"UL": "U'",
|
|
185
|
+
"DR": "D'",
|
|
186
|
+
"DL": "D"
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class CubexSolver:
|
|
191
|
+
|
|
192
|
+
def __call__(self, facets):
|
|
193
|
+
return self.solve(facets)
|
|
194
|
+
|
|
195
|
+
def solve(self, facets):
|
|
196
|
+
"""
|
|
197
|
+
EXAMPLES::
|
|
198
|
+
|
|
199
|
+
sage: # optional - rubiks
|
|
200
|
+
sage: from sage.interfaces.rubik import *
|
|
201
|
+
sage: C = RubiksCube("R U")
|
|
202
|
+
sage: CubexSolver().solve(C.facets())
|
|
203
|
+
'R U'
|
|
204
|
+
sage: C = RubiksCube("R U F L B D")
|
|
205
|
+
sage: sol = CubexSolver().solve(C.facets()); sol
|
|
206
|
+
"U' L' L' U L U' L U D L L D' L' D L' D' L D L' U' L D' L' U L' B' U' L' U B L D L D' U' L' U L B L B' L' U L U' L' F' L' F L' F L F' L' D' L' D D L D' B L B' L B' L B F' L F F B' L F' B D' D' L D B' B' L' D' B U' U' L' B' D' F' F' L D F'"
|
|
207
|
+
sage: RubiksCube(sol) == C
|
|
208
|
+
True
|
|
209
|
+
sage: C = RubiksCube("R2 F'")
|
|
210
|
+
sage: CubexSolver().solve(C.facets())
|
|
211
|
+
"R' R' F'"
|
|
212
|
+
sage: C = RubiksCube().scramble()
|
|
213
|
+
sage: sol = CubexSolver().solve(C.facets())
|
|
214
|
+
sage: C == RubiksCube(sol)
|
|
215
|
+
True
|
|
216
|
+
"""
|
|
217
|
+
s = self.format_cube(facets)
|
|
218
|
+
cmd = shlex.quote(sage.features.rubiks.cubex().absolute_filename()) + " " + s
|
|
219
|
+
child = pexpect.spawn(cmd)
|
|
220
|
+
ix = child.expect(['210.*?:', r'^5\d+(.*)'])
|
|
221
|
+
if ix == 0:
|
|
222
|
+
child.expect(['211', pexpect.EOF])
|
|
223
|
+
moves = bytes_to_str(child.before).strip().replace(',', '').split(' ')
|
|
224
|
+
return " ".join(move_map[m] for m in reversed(moves))
|
|
225
|
+
else:
|
|
226
|
+
s = child.after
|
|
227
|
+
while child.expect([r'^5\d+', pexpect.EOF]) == 0:
|
|
228
|
+
s += child.after
|
|
229
|
+
raise ValueError(bytes_to_str(s))
|
|
230
|
+
|
|
231
|
+
def format_cube(self, facets):
|
|
232
|
+
colors = sum([[i]*8 for i in range(1, 7)], [])
|
|
233
|
+
facet_colors = [0] * 54
|
|
234
|
+
for i in range(48):
|
|
235
|
+
f = facets[i]-1
|
|
236
|
+
f += (f + 4) // 8 # to compensate for the centers
|
|
237
|
+
facet_colors[f] = colors[i]
|
|
238
|
+
for i in range(6):
|
|
239
|
+
facet_colors[i*9+4] = i+1
|
|
240
|
+
return "".join(str(c) for c in facet_colors)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class DikSolver:
|
|
244
|
+
|
|
245
|
+
def __call__(self, facets):
|
|
246
|
+
return self.solve(facets)
|
|
247
|
+
|
|
248
|
+
def solve(self, facets, timeout=10, extra_time=2):
|
|
249
|
+
"""
|
|
250
|
+
EXAMPLES::
|
|
251
|
+
|
|
252
|
+
sage: # optional - rubiks
|
|
253
|
+
sage: from sage.interfaces.rubik import *
|
|
254
|
+
sage: C = RubiksCube().move("R U")
|
|
255
|
+
sage: DikSolver().solve(C.facets())
|
|
256
|
+
'R U'
|
|
257
|
+
sage: C = RubiksCube().move("R U F L B D")
|
|
258
|
+
sage: DikSolver().solve(C.facets())
|
|
259
|
+
'R U F L B D'
|
|
260
|
+
sage: C = RubiksCube().move("R2 F'")
|
|
261
|
+
sage: DikSolver().solve(C.facets())
|
|
262
|
+
"R2 F'"
|
|
263
|
+
"""
|
|
264
|
+
cube_str = self.format_cube(facets)
|
|
265
|
+
cmd = shlex.quote(sage.features.rubiks.dikcube().absolute_filename()) + " -p"
|
|
266
|
+
child = pexpect.spawn(cmd)
|
|
267
|
+
child.expect('Initialization done!')
|
|
268
|
+
child.sendline(cube_str)
|
|
269
|
+
|
|
270
|
+
# We use send(chr(4)) instead of sendeof in this case, since
|
|
271
|
+
# child.sendoef() when run in the background with the Dik solver
|
|
272
|
+
# sends a SIGTTOU which suspends the process -- this is very bad.
|
|
273
|
+
# This is only a temporary workaround, and does not fix the problem
|
|
274
|
+
# on OS X. The Dik C program itself will need to be fixed.
|
|
275
|
+
# See trac #1683. (TODO) -- willem jp, wstein, mabshoff
|
|
276
|
+
child.send(chr(4))
|
|
277
|
+
# child.sendeof()
|
|
278
|
+
|
|
279
|
+
ix = child.expect(['Solution[^\n]*:', pexpect.EOF, pexpect.TIMEOUT], timeout=timeout)
|
|
280
|
+
if ix == 0:
|
|
281
|
+
child.expect(['[^\n]+'])
|
|
282
|
+
sol = child.after.strip()
|
|
283
|
+
start_time = time.time()
|
|
284
|
+
while extra_time > time.time() - start_time:
|
|
285
|
+
ix = child.expect(['Solution[^\n]*:', pexpect.EOF, pexpect.TIMEOUT], timeout=extra_time - int(time.time() - start_time))
|
|
286
|
+
if ix == 0:
|
|
287
|
+
child.expect(['[^\n]+'])
|
|
288
|
+
sol = child.after.strip()
|
|
289
|
+
else:
|
|
290
|
+
extra_time = 0
|
|
291
|
+
# format the string into our notation
|
|
292
|
+
child.close(True)
|
|
293
|
+
sol = bytes_to_str(sol)
|
|
294
|
+
return ' '.join(self.rot_map[m[0]] + str(4 - int(m[1]))
|
|
295
|
+
for m in reversed(sol.split(' '))).replace('1', '').replace('3', "'")
|
|
296
|
+
elif ix == 1:
|
|
297
|
+
# invalid format
|
|
298
|
+
child.close(True)
|
|
299
|
+
raise ValueError(bytes_to_str(child.before))
|
|
300
|
+
else:
|
|
301
|
+
child.close(True)
|
|
302
|
+
raise RuntimeError("timeout")
|
|
303
|
+
|
|
304
|
+
def format_cube(self, facets):
|
|
305
|
+
colors = sum([[i] * 8 for i in range(1, 7)], [])
|
|
306
|
+
facet_colors = [0] * 54
|
|
307
|
+
for i in range(48):
|
|
308
|
+
f = self.facet_map.index(facets[i])
|
|
309
|
+
facet_colors[f] = colors[i]
|
|
310
|
+
# now do the centers
|
|
311
|
+
facet_colors[4] = 1
|
|
312
|
+
facet_colors[49] = 6
|
|
313
|
+
for i in range(2, 6):
|
|
314
|
+
facet_colors[16 + i * 3] = i
|
|
315
|
+
return "".join(str(c) for c in facet_colors)
|
|
316
|
+
|
|
317
|
+
facet_map = [ 1, 2, 3,
|
|
318
|
+
4, 0, 5,
|
|
319
|
+
6, 7, 8,
|
|
320
|
+
9, 10, 11, 17, 18, 19, 25, 26, 27, 33, 34, 35,
|
|
321
|
+
12, 0, 13, 20, 0, 21, 28, 0, 29, 36, 0, 37,
|
|
322
|
+
14, 15, 16, 22, 23, 24, 30, 31, 32, 38, 39, 40,
|
|
323
|
+
41, 42, 43,
|
|
324
|
+
44, 0, 45,
|
|
325
|
+
46, 47, 48,
|
|
326
|
+
]
|
|
327
|
+
|
|
328
|
+
# to compensate for different face naming
|
|
329
|
+
rot_map = dict(zip("BLURDF", "ULFRBD"))
|
|
330
|
+
|
|
331
|
+
# facet_map = [
|
|
332
|
+
# 1, 2, 3,
|
|
333
|
+
# 4, 6,
|
|
334
|
+
# 7, 8, 9,
|
|
335
|
+
# 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
|
|
336
|
+
# 23, 25, 26, 28, 29, 30, 31, 33,
|
|
337
|
+
# 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
|
|
338
|
+
# 46, 47, 48,
|
|
339
|
+
# 49, 51,
|
|
340
|
+
# 52, 53, 54,
|
|
341
|
+
# ]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-rubiks
|
|
Binary file
|
sage/libs/rubik.pyx
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-rubiks
|
sage_wheels/bin/cu2
ADDED
|
Binary file
|
sage_wheels/bin/cubex
ADDED
|
Binary file
|
sage_wheels/bin/dikcube
ADDED
|
Binary file
|
sage_wheels/bin/mcube
ADDED
|
Binary file
|
sage_wheels/bin/optimal
ADDED
|
Binary file
|
sage_wheels/bin/size222
ADDED
|
Binary file
|