rubiks_cube 1.0.3 → 1.1.0
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.
- checksums.yaml +7 -0
- data/Gemfile.lock +1 -1
- data/lib/rubiks_cube.rb +1 -0
- data/lib/rubiks_cube/solution.rb +38 -0
- data/lib/rubiks_cube/two_cycle_solution.rb +7 -29
- data/lib/rubiks_cube/version.rb +1 -1
- data/spec/rubiks_cube/solution_spec.rb +40 -0
- data/spec/rubiks_cube/two_cycle_solution_spec.rb +18 -44
- metadata +18 -33
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ba95ab7306e2711c27d79e0048fcf8d2e85b5431
|
4
|
+
data.tar.gz: e8ea3a5407c148f56c66f1ba5461dd1bbfe8720d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 224109e67b1da66903e9a2aed557a82f583732995674e23b1ddd5a75ea35d44c65fcdc7e74fee9dd26fb295c60fbeb97f519b19ad6f721f1d2218450a5c87c17
|
7
|
+
data.tar.gz: 3552b5164b043e02ac70715c6a0ba98f459d51ef401c65ebe4182e8d0a750c41ef5c4759312c6dd2f4d9a2602b47f86135555a11ef263d9dd7f694cf632d1226
|
data/Gemfile.lock
CHANGED
data/lib/rubiks_cube.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
module RubiksCube
|
2
|
+
# Abstract interface for a RubiksCube solution
|
3
|
+
#
|
4
|
+
# Must implement:
|
5
|
+
# solution: array or string of moves necessary to solve the cube
|
6
|
+
# pretty: human readable string of solution
|
7
|
+
class Solution
|
8
|
+
attr_reader :cube
|
9
|
+
|
10
|
+
# Array or String of moves necessary to solve the cube
|
11
|
+
def solution
|
12
|
+
raise "#solution unimplemented in #{self.class.name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Human readable string of solution
|
16
|
+
def pretty
|
17
|
+
raise "#pretty unimplemented in #{self.class.name}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(cube)
|
21
|
+
@cube = Cube.new(cube.state)
|
22
|
+
end
|
23
|
+
|
24
|
+
def state
|
25
|
+
cube.state
|
26
|
+
end
|
27
|
+
|
28
|
+
def solved?
|
29
|
+
cube.solved?
|
30
|
+
end
|
31
|
+
|
32
|
+
def length
|
33
|
+
(solution.respond_to?(:flatten) ? solution.flatten.join(' ') : solution)
|
34
|
+
.split.count
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -1,21 +1,8 @@
|
|
1
1
|
module RubiksCube
|
2
|
-
# Very inefficient two-cycle solving algorithm
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(cube)
|
7
|
-
@cube = Cube.new(cube.state)
|
8
|
-
end
|
9
|
-
|
10
|
-
def state
|
11
|
-
cube.state
|
12
|
-
end
|
13
|
-
|
14
|
-
def solved?
|
15
|
-
cube.solved?
|
16
|
-
end
|
17
|
-
|
18
|
-
def solve!
|
2
|
+
# Very inefficient two-cycle solving algorithm (aka bicycle solution)
|
3
|
+
# Useful for learning and blindfold
|
4
|
+
class TwoCycleSolution < Solution
|
5
|
+
def solution
|
19
6
|
@solution ||= begin
|
20
7
|
solution = []
|
21
8
|
solution << solution_for(:permutation)
|
@@ -24,15 +11,6 @@ module RubiksCube
|
|
24
11
|
end
|
25
12
|
end
|
26
13
|
|
27
|
-
def solution
|
28
|
-
solve!
|
29
|
-
@solution
|
30
|
-
end
|
31
|
-
|
32
|
-
def length
|
33
|
-
solution.flatten.join(' ').split.count
|
34
|
-
end
|
35
|
-
|
36
14
|
def pretty
|
37
15
|
solution.each_slice(3).map do |setup, correction, undo|
|
38
16
|
step = []
|
@@ -66,9 +44,9 @@ module RubiksCube
|
|
66
44
|
end
|
67
45
|
|
68
46
|
def next_orientation_location_for(cubie)
|
69
|
-
incorrect_locations_for(cubie, :orientation)
|
70
|
-
|
71
|
-
|
47
|
+
locations = incorrect_locations_for(cubie, :orientation)
|
48
|
+
locations.delete 0
|
49
|
+
locations.first
|
72
50
|
end
|
73
51
|
|
74
52
|
def next_permutation_location_for(cubie)
|
data/lib/rubiks_cube/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RubiksCube::Solution do
|
4
|
+
class TestSolution < RubiksCube::Solution
|
5
|
+
def initialize(cube, solution)
|
6
|
+
@solution = solution
|
7
|
+
super cube
|
8
|
+
end
|
9
|
+
|
10
|
+
def solution; @solution; end
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { TestSolution.new(double('Cube', state: 'nope'), solution) }
|
14
|
+
|
15
|
+
describe '#length' do
|
16
|
+
context 'when the solution is a single array' do
|
17
|
+
let(:solution) { %w(a b c) }
|
18
|
+
|
19
|
+
it 'returns the length of the solution' do
|
20
|
+
expect(subject.length).to eq 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when the solution contains multiple arrays' do
|
25
|
+
let(:solution) { [%w(a b), %w(c d), [%w(e f)]] }
|
26
|
+
|
27
|
+
it 'returns the length of the solution' do
|
28
|
+
expect(subject.length).to eq 6
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the solution is a string' do
|
33
|
+
let(:solution) { "a b c d" }
|
34
|
+
|
35
|
+
it 'returns the length of the solution' do
|
36
|
+
expect(subject.length).to eq 4
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -6,10 +6,10 @@ describe RubiksCube::TwoCycleSolution do
|
|
6
6
|
let(:cube) { RubiksCube::Cube.new state }
|
7
7
|
let(:state) { nil }
|
8
8
|
|
9
|
-
describe '#
|
9
|
+
describe '#solution' do
|
10
10
|
shared_examples_for 'a cube that can be solved' do
|
11
11
|
it 'solves the cube' do
|
12
|
-
subject.
|
12
|
+
subject.solution
|
13
13
|
expect(subject).to be_solved
|
14
14
|
end
|
15
15
|
end
|
@@ -17,7 +17,7 @@ describe RubiksCube::TwoCycleSolution do
|
|
17
17
|
it 'does not modify the original cube state' do
|
18
18
|
original_cube_state = cube.l.state
|
19
19
|
|
20
|
-
subject.
|
20
|
+
subject.solution
|
21
21
|
|
22
22
|
expect(cube.state).to eq original_cube_state
|
23
23
|
end
|
@@ -64,47 +64,21 @@ describe RubiksCube::TwoCycleSolution do
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
context 'when the cube has been solved' do
|
84
|
-
before { subject.solve! }
|
85
|
-
|
86
|
-
it 'returns the setup, algorithm, and undo moves' do
|
87
|
-
expect(subject.solution).to eq([
|
88
|
-
"", RubiksCube::Algorithms::Permutation::Edge, "",
|
89
|
-
"M2 D L2", RubiksCube::Algorithms::Permutation::Edge, "L2 D' M2",
|
90
|
-
"R2 D' R2", RubiksCube::Algorithms::Permutation::Corner, "R2 D R2",
|
91
|
-
"", RubiksCube::Algorithms::Permutation::Corner, "",
|
92
|
-
"R B", RubiksCube::Algorithms::Orientation::Edge, "B' R'",
|
93
|
-
"", RubiksCube::Algorithms::Orientation::Edge, ""
|
94
|
-
])
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
describe '#length' do
|
100
|
-
it 'returns the length of the solution' do
|
101
|
-
cube.l.r
|
102
|
-
expect(subject.length).to eq 634
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'returns zero when cube already solved' do
|
106
|
-
expect(subject.length).to be_zero
|
107
|
-
end
|
67
|
+
it 'returns the setup, algorithm, and undo moves' do
|
68
|
+
cube.perform!([
|
69
|
+
RubiksCube::Algorithms::Permutation::Edge,
|
70
|
+
RubiksCube::Algorithms::Permutation::Corner,
|
71
|
+
RubiksCube::Algorithms::Orientation::Edge,
|
72
|
+
].join(' '))
|
73
|
+
|
74
|
+
expect(subject.solution).to eq([
|
75
|
+
"", RubiksCube::Algorithms::Permutation::Edge, "",
|
76
|
+
"M2 D L2", RubiksCube::Algorithms::Permutation::Edge, "L2 D' M2",
|
77
|
+
"R2 D' R2", RubiksCube::Algorithms::Permutation::Corner, "R2 D R2",
|
78
|
+
"", RubiksCube::Algorithms::Permutation::Corner, "",
|
79
|
+
"R B", RubiksCube::Algorithms::Orientation::Edge, "B' R'",
|
80
|
+
"", RubiksCube::Algorithms::Orientation::Edge, ""
|
81
|
+
])
|
108
82
|
end
|
109
83
|
end
|
110
84
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubiks_cube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Chris Hunt
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-09-09 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,65 +27,57 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: cane-hashcheck
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: coveralls
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - '>='
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: Solve your Rubik's Cube with a two-cycle solution
|
@@ -120,48 +109,44 @@ files:
|
|
120
109
|
- lib/rubiks_cube/algorithms.rb
|
121
110
|
- lib/rubiks_cube/cube.rb
|
122
111
|
- lib/rubiks_cube/cubie.rb
|
112
|
+
- lib/rubiks_cube/solution.rb
|
123
113
|
- lib/rubiks_cube/two_cycle_solution.rb
|
124
114
|
- lib/rubiks_cube/version.rb
|
125
115
|
- rubiks_cube.gemspec
|
126
116
|
- spec/rubiks_cube/algorithms_spec.rb
|
127
117
|
- spec/rubiks_cube/cube_spec.rb
|
128
118
|
- spec/rubiks_cube/cubie_spec.rb
|
119
|
+
- spec/rubiks_cube/solution_spec.rb
|
129
120
|
- spec/rubiks_cube/two_cycle_solution_spec.rb
|
130
121
|
- spec/spec_helper.rb
|
131
122
|
homepage: https://github.com/chrishunt/rubiks-cube
|
132
123
|
licenses:
|
133
124
|
- MIT
|
125
|
+
metadata: {}
|
134
126
|
post_install_message:
|
135
127
|
rdoc_options: []
|
136
128
|
require_paths:
|
137
129
|
- lib
|
138
130
|
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
-
none: false
|
140
131
|
requirements:
|
141
|
-
- -
|
132
|
+
- - '>='
|
142
133
|
- !ruby/object:Gem::Version
|
143
134
|
version: '0'
|
144
|
-
segments:
|
145
|
-
- 0
|
146
|
-
hash: 1608407243264809376
|
147
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
-
none: false
|
149
136
|
requirements:
|
150
|
-
- -
|
137
|
+
- - '>='
|
151
138
|
- !ruby/object:Gem::Version
|
152
139
|
version: '0'
|
153
|
-
segments:
|
154
|
-
- 0
|
155
|
-
hash: 1608407243264809376
|
156
140
|
requirements: []
|
157
141
|
rubyforge_project:
|
158
|
-
rubygems_version:
|
142
|
+
rubygems_version: 2.0.3
|
159
143
|
signing_key:
|
160
|
-
specification_version:
|
144
|
+
specification_version: 4
|
161
145
|
summary: Solve your Rubik's Cube with a two-cycle solution
|
162
146
|
test_files:
|
163
147
|
- spec/rubiks_cube/algorithms_spec.rb
|
164
148
|
- spec/rubiks_cube/cube_spec.rb
|
165
149
|
- spec/rubiks_cube/cubie_spec.rb
|
150
|
+
- spec/rubiks_cube/solution_spec.rb
|
166
151
|
- spec/rubiks_cube/two_cycle_solution_spec.rb
|
167
152
|
- spec/spec_helper.rb
|