This commit is contained in:
Simon Castellan 2022-02-11 09:41:02 +01:00
commit f9762e70df
5 changed files with 13049 additions and 0 deletions

5
.ocamlformat Normal file
View File

@ -0,0 +1,5 @@
profile = ocamlformat
break-cases = fit
margin = 77
parse-docstrings = true
wrap-comments = true

2
dune Normal file
View File

@ -0,0 +1,2 @@
(executable
(name evil_wordle))

1
dune-project Normal file
View File

@ -0,0 +1 @@
(lang dune 2.9)

69
evil_wordle.ml Normal file
View File

@ -0,0 +1,69 @@
let word_list =
let f = open_in "words" in
let rec aux l =
match input_line f with
| "" -> aux l
| s -> aux (s :: l)
| exception _ -> l
in
let v = aux [] in
close_in f ; v
type result = Absent | WrongPlace | RightPlace
let evaluate ~guess ~word =
let result = Array.make (String.length word) Absent in
let word_array = Array.make 256 [] in
for i = 0 to String.length guess - 1 do
if guess.[i] = word.[i] then result.(i) <- RightPlace
else
word_array.(Char.code word.[i]) <- i :: word_array.(Char.code word.[i])
done ;
for i = 0 to String.length guess - 1 do
let ind = Char.code guess.[i] in
if result.(i) = Absent && word_array.(ind) != [] then (
result.(i) <- WrongPlace ;
word_array.(ind) <- List.tl word_array.(ind) )
done ;
result
module K = struct
type t = result array
let compare = compare
end
module M = Map.Make (K)
let evaluate_guess guess wlist =
let iter map word =
let key = evaluate ~guess ~word in
M.update key
(function None -> Some [word] | Some l -> Some (word :: l))
map
in
let map = List.fold_left iter M.empty wlist in
let fold new_pattern words (old_pattern, old_words, m) =
let n = List.length words in
if n > m then (new_pattern, words, n) else (old_pattern, old_words, m)
in
let pattern, words = M.min_binding map in
M.fold fold map (pattern, words, List.length words)
let print_pattern pat =
let p = function Absent -> "_" | WrongPlace -> "o" | RightPlace -> "X" in
String.concat "" @@ List.map p @@ Array.to_list pat
let evil_wordle () =
let rec aux word_list =
Printf.printf "Enter guess (%d words): %!" (List.length word_list) ;
let guess = input_line stdin in
let pattern, words, _ = evaluate_guess guess word_list in
if List.length words = 1 then Printf.printf "Correct !\n"
else (
Printf.printf "Wrong: %s\n" (print_pattern pattern) ;
aux words )
in
aux word_list
let () = evil_wordle ()

12972
words Normal file

File diff suppressed because it is too large Load Diff