twisty_puzzles 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +9 -0
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/LICENSE +21 -0
  5. data/README.md +32 -0
  6. data/ext/twisty_puzzles/native/extconf.rb +5 -0
  7. data/lib/twisty_puzzles/abstract_direction.rb +54 -0
  8. data/lib/twisty_puzzles/abstract_move.rb +170 -0
  9. data/lib/twisty_puzzles/abstract_move_parser.rb +45 -0
  10. data/lib/twisty_puzzles/algorithm.rb +155 -0
  11. data/lib/twisty_puzzles/algorithm_transformation.rb +33 -0
  12. data/lib/twisty_puzzles/axis_face_and_direction_move.rb +78 -0
  13. data/lib/twisty_puzzles/cancellation_helper.rb +165 -0
  14. data/lib/twisty_puzzles/color_scheme.rb +174 -0
  15. data/lib/twisty_puzzles/commutator.rb +118 -0
  16. data/lib/twisty_puzzles/compiled_algorithm.rb +48 -0
  17. data/lib/twisty_puzzles/compiled_cube_algorithm.rb +67 -0
  18. data/lib/twisty_puzzles/compiled_skewb_algorithm.rb +28 -0
  19. data/lib/twisty_puzzles/coordinate.rb +318 -0
  20. data/lib/twisty_puzzles/cube.rb +660 -0
  21. data/lib/twisty_puzzles/cube_constants.rb +53 -0
  22. data/lib/twisty_puzzles/cube_direction.rb +27 -0
  23. data/lib/twisty_puzzles/cube_move.rb +384 -0
  24. data/lib/twisty_puzzles/cube_move_parser.rb +100 -0
  25. data/lib/twisty_puzzles/cube_print_helper.rb +160 -0
  26. data/lib/twisty_puzzles/cube_state.rb +113 -0
  27. data/lib/twisty_puzzles/letter_scheme.rb +72 -0
  28. data/lib/twisty_puzzles/move_type_creator.rb +27 -0
  29. data/lib/twisty_puzzles/parser.rb +222 -0
  30. data/lib/twisty_puzzles/part_cycle_factory.rb +59 -0
  31. data/lib/twisty_puzzles/puzzle.rb +26 -0
  32. data/lib/twisty_puzzles/reversible_applyable.rb +37 -0
  33. data/lib/twisty_puzzles/rotation.rb +105 -0
  34. data/lib/twisty_puzzles/skewb_direction.rb +24 -0
  35. data/lib/twisty_puzzles/skewb_move.rb +59 -0
  36. data/lib/twisty_puzzles/skewb_move_parser.rb +73 -0
  37. data/lib/twisty_puzzles/skewb_notation.rb +147 -0
  38. data/lib/twisty_puzzles/skewb_state.rb +163 -0
  39. data/lib/twisty_puzzles/state_helper.rb +32 -0
  40. data/lib/twisty_puzzles/sticker_cycle.rb +70 -0
  41. data/lib/twisty_puzzles/twisty_puzzles_error.rb +6 -0
  42. data/lib/twisty_puzzles/utils/array_helper.rb +109 -0
  43. data/lib/twisty_puzzles/utils/string_helper.rb +26 -0
  44. data/lib/twisty_puzzles/utils.rb +7 -0
  45. data/lib/twisty_puzzles/version.rb +3 -0
  46. data/lib/twisty_puzzles.rb +5 -0
  47. metadata +249 -0
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'twisty_puzzles/abstract_direction'
4
+
5
+ module TwistyPuzzles
6
+
7
+ # Common utilities for different puzzle states.
8
+ # TODO: Remove
9
+ module StateHelper
10
+ def apply_sticker_cycle(cycle)
11
+ last_sticker = self[cycle[-1]]
12
+ (cycle.length - 1).downto(1) do |i|
13
+ self[cycle[i]] = self[cycle[i - 1]]
14
+ end
15
+ self[cycle[0]] = last_sticker
16
+ end
17
+
18
+ def apply_4sticker_cycle(cycle, direction)
19
+ raise ArgumentError unless cycle.length == 4
20
+ raise TypeError unless direction.is_a?(AbstractDirection)
21
+
22
+ if direction.double_move?
23
+ apply_sticker_cycle([cycle[0], cycle[2]])
24
+ apply_sticker_cycle([cycle[1], cycle[3]])
25
+ else
26
+ # Note that we cannot do reverse! because the values are cached.
27
+ actual_cycle = direction.value == 3 ? cycle.reverse : cycle
28
+ apply_sticker_cycle(actual_cycle)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require 'twisty_puzzles/coordinate'
5
+ require 'twisty_puzzles/cube_state'
6
+ require 'twisty_puzzles/reversible_applyable'
7
+
8
+ module TwistyPuzzles
9
+
10
+ # A sticker cycle that can be applied to a cube state.
11
+ class StickerCycle
12
+ include ReversibleApplyable
13
+
14
+ def initialize(cube_size, sticker_cycle)
15
+ @cube_size = cube_size
16
+ @sticker_cycle = sticker_cycle
17
+ end
18
+
19
+ attr_reader :cube_size, :sticker_cycle
20
+
21
+ def apply_to(cube_state)
22
+ raise TypeError unless cube_state.is_a?(CubeState)
23
+ raise ArgumentError unless cube_state.n == @cube_size
24
+
25
+ cube_state.apply_sticker_cycle(@sticker_cycle) if @sticker_cycle.length >= 2
26
+ end
27
+
28
+ def inverse
29
+ StickerCycle.new(@cube_size, @sticker_cycle.reverse)
30
+ end
31
+ end
32
+
33
+ # A set of disjoint sticker cycles that can be applied to a cube state together
34
+ class StickerCycles
35
+ include ReversibleApplyable
36
+
37
+ def initialize(cube_size, sticker_cycles)
38
+ affected_set = Set[]
39
+ sticker_cycles.each do |c|
40
+ raise TypeError unless c.is_a?(StickerCycle)
41
+
42
+ c.sticker_cycle.each do |s|
43
+ raise ArgumentError unless affected_set.add?(s)
44
+ end
45
+ end
46
+ @cube_size = cube_size
47
+ @sticker_cycles = sticker_cycles
48
+ end
49
+
50
+ attr_reader :cube_size, :sticker_cycles
51
+
52
+ def apply_to(cube_state)
53
+ raise TypeError unless cube_state.is_a?(CubeState)
54
+ raise ArgumentError unless cube_state.n == @cube_size
55
+
56
+ @sticker_cycles.each { |c| c.apply_to(cube_state) }
57
+ end
58
+
59
+ def +(other)
60
+ raise TypeError unless other.is_a?(StickerCycles)
61
+ raise ArgumentError unless @cube_size == other.cube_size
62
+
63
+ StickerCycles.new(@cube_size, @sticker_cycles + other.sticker_cycles)
64
+ end
65
+
66
+ def inverse
67
+ StickerCycles.new(@cube_size, @sticker_cycles.map(&:inverse))
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwistyPuzzles
4
+ class TwistyPuzzlesError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwistyPuzzles
4
+ module Utils
5
+ # A few array related helper methods.
6
+ module ArrayHelper
7
+ def apply_permutation(array, permutation)
8
+ raise ArgumentError unless array.length == permutation.length
9
+ raise ArgumentError unless permutation.uniq.length == permutation.length
10
+
11
+ permutation.map do |i|
12
+ raise ArgumentError unless i.is_a?(Integer) && i >= 0 && i < array.length
13
+
14
+ array[i]
15
+ end
16
+ end
17
+
18
+ def turned_equals?(left, right)
19
+ return false if left.length != right.length
20
+
21
+ (0...left.length).any? do |r|
22
+ return true if left.rotate(r) == right
23
+ end
24
+ false
25
+ end
26
+
27
+ private
28
+
29
+ def next_state_after_nil(array, state)
30
+ case state
31
+ when :start then :first_nil_part
32
+ when :first_part then :nil_middle
33
+ when :middle then :second_nil_part
34
+ when :first_nil_part, :second_nil_part, :nil_middle then state
35
+ when :second_part
36
+ raise ArgumentError,
37
+ "Cannot rotate out nils for #{array.inspect} since the nils are not contiguous."
38
+ else raise "Unknown state #{state} reached."
39
+ end
40
+ end
41
+
42
+ def next_state_after_non_nil(array, state)
43
+ case state
44
+ when :start then :first_part
45
+ when :first_nil_part then :middle
46
+ when :nil_middle then :second_part
47
+ when :first_part, :second_part, :middle then state
48
+ when :second_nil_part
49
+ raise ArgumentError,
50
+ "Cannot rotate out nils for #{array.inspect} since the nils are not contiguous."
51
+ else raise "Unknown state #{state} reached."
52
+ end
53
+ end
54
+
55
+ def next_state(state, array, element, first_part, second_part)
56
+ if element.nil?
57
+ next_state_after_nil(array, state)
58
+ else
59
+ state = next_state_after_non_nil(array, state)
60
+ current_part = state == :second_part ? second_part : first_part
61
+ current_part.push(element)
62
+ state
63
+ end
64
+ end
65
+
66
+ public
67
+
68
+ def rotate_out_nils(array)
69
+ first_part = []
70
+ second_part = []
71
+ # Valid input goes either start -> first_part -> nil_middle -> second_part
72
+ # or start -> first_nil_part -> middle -> second_nil_part
73
+ state = :start
74
+ array.each do |element|
75
+ state = next_state(state, array, element, first_part, second_part)
76
+ end
77
+ second_part + first_part
78
+ end
79
+
80
+ def check_types(array, type)
81
+ array.each { |e| raise TypeError unless e.is_a?(type) }
82
+ end
83
+
84
+ # Returns the only element of an array and raises if the array has not exactly one element.
85
+ def only(array)
86
+ raise ArgumentError, "Can't take the only element of an empty array." if array.empty?
87
+
88
+ unless array.length == 1
89
+ raise ArgumentError,
90
+ "Can't take the only element of an array with #{array.length} elements."
91
+ end
92
+
93
+ array[0]
94
+ end
95
+
96
+ def find_only(array, &block)
97
+ only(array.select(&block))
98
+ end
99
+
100
+ def replace_once(array, old_element, new_element)
101
+ raise ArgumentError unless array.count { |e| e == old_element } == 1
102
+
103
+ new_array = array.dup
104
+ new_array[new_array.index(old_element)] = new_element
105
+ new_array
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwistyPuzzles
4
+ module Utils
5
+ # A few string related helper methods.
6
+ module StringHelper
7
+ def camel_case_to_snake_case(camel_case)
8
+ camel_case.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
9
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
10
+ .downcase
11
+ end
12
+
13
+ def simple_class_name(clazz)
14
+ clazz.name.split('::').last
15
+ end
16
+
17
+ def snake_case_class_name(clazz)
18
+ camel_case_to_snake_case(simple_class_name(clazz))
19
+ end
20
+
21
+ def format_time(time_s)
22
+ format('%<time_s>.2f', time_s: time_s)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CubeTrainer
4
+ # Module that contains various utils that aren't very specific to twisty puzzles.
5
+ module Utils
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module TwistyPuzzles
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Libraries for the twisty puzzles.
4
+ module TwistyPuzzles
5
+ end
metadata ADDED
@@ -0,0 +1,249 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twisty_puzzles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bernhard F. Brodowsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rantly
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-prof
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email: bernhard.brodowsky@gmail.com
169
+ executables: []
170
+ extensions:
171
+ - ext/twisty_puzzles/native/extconf.rb
172
+ extra_rdoc_files:
173
+ - README.md
174
+ files:
175
+ - CHANGELOG.md
176
+ - CODE_OF_CONDUCT.md
177
+ - LICENSE
178
+ - README.md
179
+ - ext/twisty_puzzles/native/extconf.rb
180
+ - lib/twisty_puzzles.rb
181
+ - lib/twisty_puzzles/abstract_direction.rb
182
+ - lib/twisty_puzzles/abstract_move.rb
183
+ - lib/twisty_puzzles/abstract_move_parser.rb
184
+ - lib/twisty_puzzles/algorithm.rb
185
+ - lib/twisty_puzzles/algorithm_transformation.rb
186
+ - lib/twisty_puzzles/axis_face_and_direction_move.rb
187
+ - lib/twisty_puzzles/cancellation_helper.rb
188
+ - lib/twisty_puzzles/color_scheme.rb
189
+ - lib/twisty_puzzles/commutator.rb
190
+ - lib/twisty_puzzles/compiled_algorithm.rb
191
+ - lib/twisty_puzzles/compiled_cube_algorithm.rb
192
+ - lib/twisty_puzzles/compiled_skewb_algorithm.rb
193
+ - lib/twisty_puzzles/coordinate.rb
194
+ - lib/twisty_puzzles/cube.rb
195
+ - lib/twisty_puzzles/cube_constants.rb
196
+ - lib/twisty_puzzles/cube_direction.rb
197
+ - lib/twisty_puzzles/cube_move.rb
198
+ - lib/twisty_puzzles/cube_move_parser.rb
199
+ - lib/twisty_puzzles/cube_print_helper.rb
200
+ - lib/twisty_puzzles/cube_state.rb
201
+ - lib/twisty_puzzles/letter_scheme.rb
202
+ - lib/twisty_puzzles/move_type_creator.rb
203
+ - lib/twisty_puzzles/parser.rb
204
+ - lib/twisty_puzzles/part_cycle_factory.rb
205
+ - lib/twisty_puzzles/puzzle.rb
206
+ - lib/twisty_puzzles/reversible_applyable.rb
207
+ - lib/twisty_puzzles/rotation.rb
208
+ - lib/twisty_puzzles/skewb_direction.rb
209
+ - lib/twisty_puzzles/skewb_move.rb
210
+ - lib/twisty_puzzles/skewb_move_parser.rb
211
+ - lib/twisty_puzzles/skewb_notation.rb
212
+ - lib/twisty_puzzles/skewb_state.rb
213
+ - lib/twisty_puzzles/state_helper.rb
214
+ - lib/twisty_puzzles/sticker_cycle.rb
215
+ - lib/twisty_puzzles/twisty_puzzles_error.rb
216
+ - lib/twisty_puzzles/utils.rb
217
+ - lib/twisty_puzzles/utils/array_helper.rb
218
+ - lib/twisty_puzzles/utils/string_helper.rb
219
+ - lib/twisty_puzzles/version.rb
220
+ homepage: https://github.com/Lykos/twisty_puzzles
221
+ licenses:
222
+ - MIT
223
+ metadata:
224
+ homepage_uri: https://github.com/Lykos/twisty_puzzles
225
+ source_code_uri: https://github.com/Lykos/twisty_puzzles
226
+ changelog_uri: https://github.com/Lykos/twisty_puzzles/blob/master/CHANGELOG.md
227
+ post_install_message:
228
+ rdoc_options: []
229
+ require_paths:
230
+ - lib
231
+ required_ruby_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: 2.3.0
236
+ required_rubygems_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 2.7.6.2
244
+ signing_key:
245
+ specification_version: 4
246
+ summary: Gem for my cube_trainer rails app. Some things are better left in a separate
247
+ gem with no rails, e.g. native extensions. The main purpose is to support my Rails
248
+ app, but if it's useful for someone else, feel free to use it at your own risk.
249
+ test_files: []