~srpablo/squardle_bot

b9cd642b1736ad824238055425ec7b892f64dc89 — Pablo Meier 2 years ago 87d9ba6
oCamlfmt
M src/squardle_solver_lib/datatypes.ml => src/squardle_solver_lib/datatypes.ml +2 -0
@@ 45,6 45,7 @@ module Tag = struct
      else
        -1


    let sexp_of_t { guess = g1; color = c1 } =
      Sexp.List [Sexp.Atom g1; Sexp.Atom (show_tag_color c1)]
  end


@@ 105,6 106,7 @@ let row_offsets = function
  | 2 -> [16; 17; 18; 19; 20]
  | _ -> failwith "invalid row index"


let col_offsets = function
  | 0 -> [0; 5; 8; 13; 16]
  | 1 -> [2; 6; 10; 14; 18]

M src/squardle_solver_lib/game.ml => src/squardle_solver_lib/game.ml +3 -0
@@ 12,6 12,7 @@ let make_new_tags exploded_string color_collection =
  | List.Or_unequal_lengths.Ok lst -> List.map ~f:tag_from_tuple lst
  | List.Or_unequal_lengths.Unequal_lengths -> failwith "unequal lengths"


(* mad inefficient but who cares lol *)
let sub_value_at lst i new_val = List.take lst i @ [new_val] @ List.drop lst (i + 1)



@@ 26,6 27,7 @@ let add_tags_generic board offsets tags =
      in
      sub_value_at accum offset tile)


let set_row_tiles cursor row board = add_tags_generic board (row_offsets cursor) row

let set_col_tiles ~cursor ~col board = add_tags_generic board (col_offsets cursor) col


@@ 43,6 45,7 @@ let record_guess { board; cursor; num_guesses } guess row_colors col_colors =
  let new_cursor = (cursor + 1) mod 3 in
  { board = new_board; cursor = new_cursor; num_guesses = num_guesses - 1 }


let starting_game =
  let empty_tag_set = Set.of_list (module Tag) [] in
  let starting_board =

A src/squardle_solver_lib/game.mli => src/squardle_solver_lib/game.mli +5 -0
@@ 0,0 1,5 @@
open Datatypes

val record_guess : game -> string -> tag_color list -> tag_color list -> game

val starting_game : game

M src/squardle_solver_lib/main.ml => src/squardle_solver_lib/main.ml +1 -0
@@ 6,6 6,7 @@ let rec game_loop game =
  let () = Printf.printf "We suggest: %s\n" suggestion in
  game_loop advanced_state


(** Runs the CLI all *)
let play () =
  Printf.printf "Good luck!\n";

M src/squardle_solver_lib/suggest.ml => src/squardle_solver_lib/suggest.ml +7 -0
@@ 60,11 60,13 @@ let as_exclusion_regex str =
  else
    String.concat ["[^"; str; "]"]


let pattern_char_for exclusion_pattern tile =
  match tile with
  | Solved (x, _) -> x
  | Unsolved _ -> exclusion_pattern


let winnow_down_list valid_guess_list tiles guess_type global_blacks =
  let global_exclusions = tag_set_to_exclusion_string guess_type global_blacks in
  let exclusion_pattern =


@@ 78,6 80,7 @@ let winnow_down_list valid_guess_list tiles guess_type global_blacks =
  let regex = Re2.create_exn regex_pattern in
  List.filter_map valid_guess_list ~f:(fun x -> if Re2.matches regex x then Some x else None)


(* Should have made these maps lol *)
let find_tag_for_char tagset chr return_if_true =
  let filtered =


@@ 87,6 90,7 @@ let find_tag_for_char tagset chr return_if_true =
  | [] -> None
  | _ -> Some return_if_true


let score_spot ojs_reds_yellows whites (tile, guess_char) =
  match tile with
  | Solved _ -> 10


@@ 102,6 106,7 @@ let score_spot ojs_reds_yellows whites (tile, guess_char) =
          let is_global_white = find_tag_for_char whites guess_char 2 in
          Option.value ~default:0 is_oj_but_not_this + Option.value ~default:0 is_global_white)


let score_guess tiles ojs_reds_yellows considered_whites guess =
  let tiles_and_chars = List.zip_exn tiles (explode_string guess) in
  let score =


@@ 111,6 116,7 @@ let score_guess tiles ojs_reds_yellows considered_whites guess =
  in
  guess, score


let tags_of_color color lst_of_tiles =
  lst_of_tiles
  |> List.map ~f:tags_of


@@ 119,6 125,7 @@ let tags_of_color color lst_of_tiles =
  |> List.filter ~f:(color_match color)
  |> Set.of_list (module Tag)


(**
 * We do dynamic regex construction, and really naive matching. If we had Elixir Streams, that'd
 * be pretty dope, but I trust OCaml is p fast. We'll look at the tag lists, construct regexes

M src/squardle_solver_lib/wordlist.mli => src/squardle_solver_lib/wordlist.mli +1 -1
@@ 1,3 1,3 @@
val valid_guesses : string list
(** We hardcode these in. Making an .mli file to see if it
    helps with incremental compilation. *)
val valid_guesses: string list