package knights_tour

  1. Overview
  2. Docs
Solves the Knight's tour puzzle; and others

Install

dune-project
 Dependency

Authors

Maintainers

Sources

knights_tour-0.0.2.tbz
sha256=8362f846492183e83e1901f73933d772c193c08b8a375480c28d0f23f0d29e11
sha512=5fe79ac95a3e5a2e01d429665fa7a8bfef422d9843758b35db2c8efc299c762c1d22974266f5e8802ee6f81e09223aa90476824d1fd93540c473cc8a357d38a3

doc/src/knights_tour.pentominos/polyomino.ml.html

Source file polyomino.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
open Knights_tour

type t = {
  name: char;
  variants: PointSet.t list
}

let name {name;_} = name

let points {variants;_} = match variants with 
  | canonical_rep::_ -> canonical_rep
  | [] -> failwith "Bug: this should not be possible. A polymino should have at least 1 variant."

let order p = PointSet.cardinal (points p)

let compare p1 p2 = PointSet.compare (points p1) (points p2)

let variants {variants;_}= variants

let to_string p = p |> points |> PointSet.to_string

let adjacent_point points = PointSet.adjacent points 
  |> PointSet.to_seq |> Searchspace.of_seq

let rec of_order_aux n = 
  let open Searchspace in (
    if n<1 then
      failwith "Illegal argument: order must >=1"
    else if n=1 then
      PointSet.of_string "#" |> PointSet.normalize |> return
    else (
      let with_duplicates = of_order_aux (n - 1) |=> (fun smaller_piece ->
        adjacent_point smaller_piece |=> (fun adjacent_point ->
          PointSet.add adjacent_point smaller_piece
          |> PointSet.normalize
          |> return
        )
      ) in 
      with_duplicates |> Searchspace.no_dup PointSet.compare
    )
  )

let of_order n =
  let nextName = ref 'A' in
  of_order_aux n 
  |> Searchspace.map (fun points ->
    let p = {name = !nextName; variants = PointSet.variants points} in
    nextName := (Char.code !nextName) + 1 |> Char.chr;
    p
  )
  |> Searchspace.to_seq
  |> List.of_seq

let print_polyos n =
  let polys = of_order n in
  polys |> List.iteri (fun i piece -> 
    let open Printf in
    printf "=============\n";
    printf "%d:\n" (i+1);
    printf "-------------\n";
    printf "%s\n" (to_string piece)
  ) 

let pin_symmetry polyos =
  let to_change = polyos |> List.find (fun p -> List.length (variants p) = 8) in
  polyos |> List.map (fun p ->
    if p.name = to_change.name then
      {p with variants = [List.hd p.variants]}
    else 
      p (*no change *)
  )

let%expect_test "pin_symmetry of tetro-minos" =
  let tetros = of_order 4 |> pin_symmetry in
  tetros |> List.iter (fun tetro ->
    Printf.printf "===================\nvariants: %d\n\n%s"
      (List.length (variants tetro))
      (to_string tetro)
  );
  [%expect {|
    ===================
    variants: 2

    ####
    ===================
    variants: 1

    ###
    #..
    ===================
    variants: 4

    ###
    .#.
    ===================
    variants: 1

    ##
    ##
    ===================
    variants: 4

    ##.
    .## |}]

let%expect_test "mono-minos" =
  print_polyos 1
  ;[%expect{|
    =============
    1:
    -------------
    # |}]

let%expect_test "do-minos" =
  print_polyos 2
  ;[%expect{|
    =============
    1:
    -------------
    ## |}]

let%expect_test "3-minos" =
  print_polyos 3
  ;[%expect{|
    =============
    1:
    -------------
    ###

    =============
    2:
    -------------
    ##
    #. |}]

let%expect_test "tetro-minos" =
  print_polyos 4
  ;[%expect{|
    =============
    1:
    -------------
    ####

    =============
    2:
    -------------
    ###
    #..

    =============
    3:
    -------------
    ###
    .#.

    =============
    4:
    -------------
    ##
    ##

    =============
    5:
    -------------
    ##.
    .## |}]

let%expect_test "pento-minos" =
  print_polyos 5
  ;[%expect{|
    =============
    1:
    -------------
    #####

    =============
    2:
    -------------
    ####
    #...

    =============
    3:
    -------------
    ####
    .#..

    =============
    4:
    -------------
    ###
    ##.

    =============
    5:
    -------------
    ###
    #.#

    =============
    6:
    -------------
    ###
    #..
    #..

    =============
    7:
    -------------
    ###
    .#.
    .#.

    =============
    8:
    -------------
    ###.
    ..##

    =============
    9:
    -------------
    ##.
    .##
    .#.

    =============
    10:
    -------------
    ##.
    .##
    ..#

    =============
    11:
    -------------
    ##.
    .#.
    .##

    =============
    12:
    -------------
    .#.
    ###
    .#. |}]

let pp_poly out poly =
  let open Format in
  pp_open_vbox out 5;
  fprintf out "\n%s" (to_string poly);
  pp_close_box out ()

OCaml

Innovation. Community. Security.