Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ocaml
(* run "./EzSudoku n" to generate template .ml file to fill with your n^2*n^2 sudoku problem
You probably shouldn't put anything bigger than 3. Or 2 in fact... *)
let size =
try int_of_string Sys.argv.(1) with
| _ -> 2
let sq_size = size * size
let name i = Char.chr (i + Char.code 'a')
let name2 i =
if sq_size * sq_size > 26 then
let s = Printf.sprintf "%c%c" (name (i / 26)) (name (i mod 26)) in
match s with
| "as" -> "as_"
| s -> s
else
Printf.sprintf "%c" (name i)
let () =
for i = 1 to sq_size do
Format.printf "type r%i@." i;
Format.printf "type c%i@." i;
Format.printf "type s%i@.@." i
done
let () = Format.printf "type ('row, 'column, 'square) position@."
let print_position n =
Format.printf "\
type %s = (r%i, c%i, s%i) position\
@."
(name2 n)
(1 + n mod sq_size)
(1 + n / sq_size)
(1 + n mod size + ((n / sq_size) mod size) * size)
let () =
Format.printf "@.";
for i = 0 to sq_size * sq_size - 1 do
print_position i
done;
Format.printf "@."
let fields ppf () =
for i = 0 to sq_size - 1 do
Format.fprintf ppf "@ %c : '%c;" (name i) (name i)
done
let const name =
Format.printf "@[<hov 2>type 'fields %s constraint 'fields =@ @[<hov 2><%a >@]@]@."
name fields ()
let () =
const "row ";
const "column";
const "square";
Format.printf "@."
let () =
Format.printf "type ('position, 'row, 'column, 'square) symbol =@."
let symb n =
Printf.printf " \
| %c : (('r, 'c, 's) position,\n \
< %c : 'r; .. > row,\n \
< %c : 'c; .. > column,\n \
< %c : 's; .. > square)\n \
symbol\n\n%!"
(Char.uppercase_ascii (name n))
(name n)
(name n)
(name n)
let () =
for i = 0 to sq_size - 1 do
symb i
done;
Format.printf "@."
let field i =
Format.printf
" (%s, 'row%i, 'column%i, 'square%i) symbol%s@."
(name2 i)
(1 + i / sq_size)
(1 + i mod sq_size)
(1 + (i / size) mod size
+ size * (i / (size * sq_size)))
(if i = sq_size * sq_size - 1 then "" else " *");
if ((i+1) mod sq_size) = 0 then Format.printf "@."
let () =
Format.printf "type grid =@. Grid :@.";
for i = 0 to sq_size * sq_size - 1 do
field i
done;
Format.printf " -> grid@."
let empty_grid ppf () =
for i = 1 to size do
for j = 1 to size do
Format.fprintf ppf " ";
for k = 1 to size do
for l = 1 to size do
Format.fprintf ppf "_";
if (i <> size) || (j <> size) || (k <> size) || (l <> size) then
Format.fprintf ppf ", ";
()
done;
Format.fprintf ppf " "
done;
Format.fprintf ppf "@."
done;
Format.fprintf ppf "@."
done
let () =
Format.printf "@.(* Fill this grid with your problem *)@.";
Format.printf "let g x =@. match x with@. | Grid (@.%a ) -> .@. | _ -> ()@."
empty_grid ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment