Merge branch 'minimum-excludant-other-versions' into 'master'
[why3.git] / examples_in_progress / sudoku_reloaded / main.ml
blobf91d19253bd50713b620c51d13cff0e4c202f135
2 open Why3extract
3 open Format
5 let usage () =
6 eprintf "Sudoku: solve a Sudoku puzzle@.";
7 eprintf "Usage: %s <comma separated sequence of 81 non-zero digits>@." Sys.argv.(0);
8 exit 2
10 let input =
11 if Array.length Sys.argv <> 2 then usage ();
12 Sys.argv.(1)
14 let input_grid =
15 if String.length input <> 161 then usage ();
16 for i=0 to 79 do
17 if input.[i+i+1] <> ',' then usage ();
18 done;
19 let a = Array.make 81 0 in
20 for i=0 to 80 do
21 match input.[i+i] with
22 | '0'..'9' as c -> a.(i) <- Char.code c - Char.code '0'
23 | _ -> usage ()
24 done;
27 let print_grid fmt a =
28 fprintf fmt "@[";
29 for i=0 to 80 do
30 if i mod 9 = 8
31 then fprintf fmt "%d@\n" a.(i)
32 else fprintf fmt "%d " a.(i)
33 done;
34 fprintf fmt "@]"
36 let () =
37 let sudoku = Sudoku_reloaded__TheClassicalSudokuGrid.classical_sudoku () in
38 printf "Problem: %a@." print_grid input_grid;
39 let a = Sudoku_reloaded__Solver.solve sudoku input_grid
41 printf "Solution: %a@." print_grid a
44 test:
46 2,0,9,0,0,0,0,1,0,0,0,0,0,6,0,0,0,0,0,5,3,8,0,2,7,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,7,5,0,0,3,0,4,1,2,0,8,9,0,0,0,0,4,0,9,0,0,2,0,8,0,0,0,0,1,0,0,5,0,0,0,0,0,0,0,7,6
48 that is
50 2 0 9 0 0 0 0 1 0
51 0 0 0 0 6 0 0 0 0
52 0 5 3 8 0 2 7 0 0
53 3 0 0 0 0 0 0 0 0
54 0 0 0 0 7 5 0 0 3
55 0 4 1 2 0 8 9 0 0
56 0 0 4 0 9 0 0 2 0
57 8 0 0 0 0 1 0 0 5
58 0 0 0 0 0 0 0 7 6
60 should give:
62 2 6 9 3 5 7 8 1 4
63 1 8 7 9 6 4 5 3 2
64 4 5 3 8 1 2 7 6 9
65 3 7 5 6 4 9 2 8 1
66 9 2 8 1 7 5 6 4 3
67 6 4 1 2 3 8 9 5 7
68 7 1 4 5 9 6 3 2 8
69 8 3 6 7 2 1 4 9 5
70 5 9 2 4 8 3 1 7 6
72 Other tests:
74 0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,8,5,0,0,1,0,2,0,0,0,0,0,0,0,5,0,7,0,0,0,0,0,4,0,0,0,1,0,0,0,9,0,0,0,0,0,0,0,5,0,0,0,0,0,0,7,3,0,0,2,0,1,0,0,0,0,0,0,0,0,4,0,0,0,9