(* Thomas Nagy, 2005 *) open Facile;; open Easy;; let print _puzzle = let n = 9 in for i = 0 to n - 1 do for j = 0 to n - 1 do let c = Fd.int_value _puzzle.(9*i+j) in Printf.printf "%d " c done; print_newline () done; flush stdout;; let puzzle = [| 5; 3; 0; 0; 7; 0; 0; 0; 0; 6; 0; 0; 1; 9; 5; 0; 0; 0; 0; 9; 8; 0; 0; 0; 0; 6; 0; 8; 0; 0; 0; 6; 0; 0; 0; 3; 4; 0; 0; 8; 0; 3; 0; 0; 1; 7; 0; 0; 0; 2; 0; 0; 0; 6; 0; 6; 0; 0; 0; 0; 2; 8; 0; 0; 0; 0; 4; 1; 9; 0; 0; 5; 0; 0; 0; 0; 8; 0; 0; 7; 9; |];; let myarr = Fd.array 81 1 9;; (* fixed values *) for i=0 to 80 do let value = Array.get puzzle i in if value > 0 then Cstr.post (fd2e myarr.(i) =~ i2e value); done;; (* constraints horizontal, vertical and squares *) let chooser i x y = if i=0 then x*9+y else if i=1 then x+y*9 else (27*(x/3)+3*(x mod 3)) + (9*(y/3)+(y mod 3)) ;; (* add the constraints *) for c=0 to 2 do for i=0 to 8 do let arr = Array.init 9 (fun k -> myarr.(chooser c i k)) in Cstr.post (Alldiff.cstr arr) done done;; (* solve *) let goal = Goals.GlArray.labeling myarr in if (Goals.solve goal) then print myarr else failwith "no solution found" ;;