-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.rb
executable file
·66 lines (52 loc) · 1.42 KB
/
sudoku.rb
1
2
3
4
5
6
7
8
9
10
11
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
61
62
63
64
65
66
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), "lib")
require "sudoku"
def print_usage
puts "Usage: #{$0} -f <puzzle_file>"
puts " #{$0} -p <puzzle_string>"
exit 1
end
if __FILE__ == $0
require 'getoptlong'
opts = GetoptLong.new(
["--puzzle", "-p", GetoptLong::REQUIRED_ARGUMENT ],
["--file", "-f", GetoptLong::REQUIRED_ARGUMENT ],
["--turns", "-t", GetoptLong::REQUIRED_ARGUMENT ],
["--html", "-h", GetoptLong::REQUIRED_ARGUMENT ]
)
file_in = nil
html_out = nil
puzzle_string = nil
max_turns = nil
opts.each do |opt, arg|
case opt
when "--file"
file_in = arg
when "--html"
html_out = arg
when "--puzzle"
puzzle_string = arg
when "--turns"
max_turns = arg.to_i
end
end
print_usage if file_in.nil? and puzzle_string.nil?
if file_in
puzzle_string = File.read(file_in)
end
b = Sudoku::Board.new(puzzle_string)
puts b
solver = Sudoku::HeuristicSolver.new(b)
solver.solve(max_turns) do |i, cell|
puts
puts "#{i}: #{cell} = #{cell.value}"
puts
puts b
end
if html_out
html_file = File.new("#{File.dirname(__FILE__)}/#{html_out}", "w")
html_file.write b.to_html
puts "Wrote puzzle to #{html_file.path}"
end
exit (b.solved?) ? 0 : 1
end