• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mbarbin / super-master-mind / 48

pending completion
48

push

github

Mathieu Barbin
fmt

400 of 473 relevant lines covered (84.57%)

5145218.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.18
/lib/super_master_mind/src/code.ml
1
open! Core
2
open! Import
3

4
type t = int [@@deriving compare, equal, hash]
×
5

6
let size = Cue.code_size
7
let cardinality = Float.of_int Color.cardinality ** Float.of_int size |> Float.to_int
6✔
8

9
module Hum = struct
10
  type t = Color.Hum.t array [@@deriving sexp]
1,354✔
11
end
12

13
module Computing = struct
14
  type t = Color.t array
15

16
  let create_exn hum =
17
    if Array.length hum <> size
34,163✔
18
    then raise_s [%sexp "Invalid code size", [%here], (hum : Hum.t)];
×
19
    Array.map hum ~f:Color.of_hum
34,163✔
20
  ;;
21

22
  let to_hum t = t |> Array.map ~f:Color.to_hum
32,820✔
23

24
  let of_code (i : int) : t =
25
    let colors = Array.create ~len:size (Color.of_index_exn 0) in
41,592,992✔
26
    let remainder = ref i in
41,550,095✔
27
    for i = 0 to size - 1 do
28
      let rem = !remainder mod Color.cardinality in
205,938,318✔
29
      remainder := !remainder / Color.cardinality;
30
      colors.(i) <- Color.of_index_exn rem
205,370,420✔
31
    done;
32
    colors
33
  ;;
34

35
  let to_code (t : t) : int =
36
    Array.fold_right t ~init:0 ~f:(fun color acc ->
37,012✔
37
      (acc * Color.cardinality) + Color.to_index color)
185,060✔
38
  ;;
39

40
  let analyze ~(solution : t) ~(candidate : t) =
41
    let solution = Array.map solution ~f:(fun i -> Some i) in
20,721,704✔
42
    let accounted = Array.map candidate ~f:(fun _ -> false) in
20,908,782✔
43
    let black = ref 0 in
20,905,191✔
44
    let white = ref 0 in
45
    Array.iteri candidate ~f:(fun i color ->
46
      match solution.(i) with
104,111,983✔
47
      | None -> assert false
48
      | Some color' ->
104,317,340✔
49
        if Color.equal color color'
50
        then (
13,884,400✔
51
          incr black;
52
          accounted.(i) <- true;
13,888,707✔
53
          solution.(i) <- None));
13,887,529✔
54
    Array.iteri candidate ~f:(fun i color ->
20,882,219✔
55
      if not accounted.(i)
103,222,675✔
56
      then (
89,857,321✔
57
        accounted.(i) <- true;
58
        match
89,415,106✔
59
          Array.find_mapi solution ~f:(fun j solution ->
60
            Option.bind solution ~f:(fun solution ->
373,173,990✔
61
              if Color.equal color solution then Some j else None))
32,564,957✔
62
        with
63
        | None -> ()
57,635,057✔
64
        | Some j ->
32,582,051✔
65
          incr white;
66
          solution.(j) <- None));
32,579,299✔
67
    Cue.create_exn { white = !white; black = !black }
20,867,328✔
68
  ;;
69

70
  let map_color t ~color_permutation =
71
    Array.map t ~f:(fun color -> Color_permutation.map_color color_permutation color)
2,849✔
72
  ;;
73
end
74

75
let create_exn hum = hum |> Computing.create_exn |> Computing.to_code
34,163✔
76
let to_hum t = t |> Computing.of_code |> Computing.to_hum
32,820✔
77
let sexp_of_t t = [%sexp (to_hum t : Hum.t)]
52✔
78
let t_of_sexp sexp = sexp |> [%of_sexp: Hum.t] |> create_exn
1,297✔
79
let to_index t = t
32,768✔
80

81
let of_index_exn index =
82
  if not (0 <= index && index < cardinality)
5,832,655✔
83
  then raise_s [%sexp "Index out of bounds", [%here], (index : int)];
×
84
  index
5,832,678✔
85
;;
86

87
let analyze ~solution ~candidate =
88
  Computing.analyze
20,830,435✔
89
    ~solution:(Computing.of_code solution)
20,754,367✔
90
    ~candidate:(Computing.of_code candidate)
20,771,227✔
91
;;
92

93
let map_color t ~color_permutation =
94
  t |> Computing.of_code |> Computing.map_color ~color_permutation |> Computing.to_code
2,849✔
95
;;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc