Class: Scrambler::RandomState::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/scrambler/random_state/solver.rb

Constant Summary collapse

SOLVED_PERMUTATION =
[0, 1, 2, 3, 4, 5, 6]
SOLVED_ORIENTATION =
[0] * 7
TURNS =
[:R, :U, :F]
MODS =
["", "2", "'"]

Instance Method Summary collapse

Constructor Details

#initializeSolver

Returns a new instance of Solver.



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
# File 'lib/scrambler/random_state/solver.rb', line 12

def initialize
  @permutation_map = []
  SOLVED_PERMUTATION.permutation.each do |p|
    @permutation_map[CornerPermutation.new(p).to_i] = { :R => CornerPermutation.new(p).turn!(:R).to_i,
                                                        :U => CornerPermutation.new(p).turn!(:U).to_i,
                                                        :F => CornerPermutation.new(p).turn!(:F).to_i,
                                                        :length => -1
                                                      }
  end

  @permutation_map[123456][:length] = 0
  7.times do |l|
    SOLVED_PERMUTATION.permutation.each do |p|
      if @permutation_map[CornerPermutation.new(p).to_i][:length] == l
        [:R, :U, :F].each do |m|
          q = CornerPermutation.new(p).to_i
          3.times do |c|
            q = @permutation_map[q][m]
            @permutation_map[q][:length] = l + 1 if @permutation_map[q][:length] == -1
          end
        end
      end
    end
  end

  @orientation_map = []
  (3**7).times do |i|
    @orientation_map[i] = { :R => CornerOrientation.new(i).turn!(:R).to_i,
                            :U => CornerOrientation.new(i).turn!(:U).to_i,
                            :F => CornerOrientation.new(i).turn!(:F).to_i,
                            :length => -1
                          }
  end

  @orientation_map[0][:length] = 0
  6.times do |l|
    (3**7).times do |o|
      if @orientation_map[o][:length] == l
        [:R, :U, :F].each do |m|
          q = o
          3.times do |c|
            q = @orientation_map[q][m]
            @orientation_map[q][:length] = l + 1 if @orientation_map[q][:length] == -1
          end
        end
      end
    end
  end
end

Instance Method Details

#solve(corner_permutation, corner_orientation) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/scrambler/random_state/solver.rb', line 62

def solve(corner_permutation, corner_orientation)
  @solution = []
  move_limit = -1
  begin
    move_limit += 1
  end while !search(corner_permutation.to_i, corner_orientation.to_i, move_limit)
  @solution.map { |t| t[0].to_s + t[1] }.join " "
end