Class: Rudoku

Inherits:
Object
  • Object
show all
Defined in:
lib/rudoku.rb

Instance Method Summary collapse

Constructor Details

#initialize(sudoku) ⇒ Rudoku



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rudoku.rb', line 2

def initialize(sudoku)
  @n = sudoku.size
  @sqrt_n = Math.sqrt(@n).to_i
  raise "wrong sudoku size" unless @sqrt_n * @sqrt_n == @n

  @arr = sudoku.collect { |row|
    (0...@n).collect { |i|
      ((1..@n) === row[i]) ? [row[i]] : (1..@n).to_a
    }
  }

  @rfix = Array.new(@n) { [] }
  @cfix = Array.new(@n) { [] }
  @bfix = Array.new(@n) { [] }
  @n.times { |r| @n.times { |c| update_fix(r, c) } }

  [@rfix, @cfix, @bfix].each { |fix| fix.each { |x|
    unless x.size == x.uniq.size
      raise "non-unique numbers in row, col or box"
    end
  } }
end

Instance Method Details

#backtrack_solveObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rudoku.rb', line 41

def backtrack_solve
  if (res = solve) == :unknown
    r, c = 0, 0
    @rfix.each_with_index { |rf, r|
      break if rf.size < @n
    }
    @arr[r].each_with_index { |x, c|
      break if x.size > 1
    }
    partial = to_a
    solutions = []
    @arr[r][c].each { |guess|
      partial[r][c] = guess
      rsolver = Rudoku.new(partial)
      case rsolver.backtrack_solve
      when :multiple_solutions
      initialize(rsolver.to_a)
      return :multiple_solutions
      when :solved
      solutions << rsolver
      end
    }
    if solutions.empty?
      return :impossible
    else
      initialize(solutions[0].to_a)
      return solutions.size > 1 ? :multiple_solutions : :solved
    end
  end
  res
end

#deduceObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rudoku.rb', line 107

def deduce
  success = false
  [:col_each, :row_each, :box_each].each { |meth|
    @n.times { |i|
      u = uniqs_in(meth, i)
      unless u.empty?
        send(meth, i) { |x|
          if x.size > 1 && ((u2 = u & x).size == 1)
            success = true
            u2
          else
            nil
          end
        }
        return success if success
      end
    }
  }
  success
end

#finished?Boolean



86
87
88
89
# File 'lib/rudoku.rb', line 86

def finished?
  @arr.each { |row| row.each { |x| return false if x.size > 1 } }
  true
end

#reduceObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rudoku.rb', line 91

def reduce
  success = false
  @n.times { |r| @n.times { |c|
    if (sz = @arr[r][c].size) > 1
      @arr[r][c] = @arr[r][c] -
      (@rfix[r] | @cfix[c] | @bfix[rc2box(r, c)])
      raise "impossible to solve" if @arr[r][c].empty?
      if @arr[r][c].size < sz
        success = true
        update_fix(r, c)
      end
    end
  } }
  success
end

#solveObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rudoku.rb', line 25

def solve
  begin
    until finished?
      progress = false
      while reduce
        progress = true
      end
      progress = true if deduce
      return :unknown unless progress
    end
    :solved
  rescue
    :impossible
  end
end

#to_aObject



73
74
75
76
77
# File 'lib/rudoku.rb', line 73

def to_a
  @arr.collect { |row| row.collect { |x|
    (x.size == 1) ? x[0] : nil
  } }
end

#to_sObject



79
80
81
82
83
84
# File 'lib/rudoku.rb', line 79

def to_s
  fw = @n.to_s.size
  to_a.collect { |row| row.collect { |x|
    (x ? x.to_s : "_").rjust(fw)
  }.join " " }.join "\n"
end