package knights_tour

  1. Overview
  2. Docs

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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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;
    if !nextName='[' then  
      nextName := 'a'; 
    p
  )
  |> Searchspace.to_seq
  |> List.of_seq

let randomize polys = 
  polys 
  |> List.map (fun poly -> 
    {poly with variants=poly.variants |> Randomize.list}
  )
  |> Randomize.list

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 ()

let save_fmt out polyos =
  polyos |> List.iter (fun polyo ->
    Format.fprintf out "%c:\n" polyo.name;
    variants polyo |> List.iter (fun variant ->
      PointSet.to_string variant 
      |> Format.fprintf out "%s\n"
    );
    Format.fprintf out "\n"
  );
  Format.fprintf out "---\n"

let save out polyos =
  let formatter = Format.formatter_of_out_channel out in
  save_fmt formatter polyos;
  Format.pp_print_flush formatter ()

let%expect_test "save polyos" = 
  let out = stdout in
  of_order 3 |> save out
  ; [%expect{|
    A:
    ###

    #
    #
    #


    B:
    ##
    #.

    ##
    .#

    #.
    ##

    .#
    ##


    --- |}] 

let load_variant first_line input =
  let lines = Lines.load_list "" Lines.load_line first_line input in
  let image = List.fold_left (fun l1 l2 -> l1 ^ "\n" ^ l2) "" lines in
  PointSet.of_string image

let load_poly first_line input =
  let name = String.get first_line 0 in
  match Seq.uncons input with
  | None -> failwith "Unexpected end of input"
  | Some (first_line, input) ->
      let variants = Lines.load_list "" load_variant first_line input in {
        name; variants
      }

let load_lines input =
  Seq.uncons input |> function
  | None -> failwith "Unexpected end of input"
  | Some (first_line, input) -> Lines.load_list "---" load_poly first_line input

let load input = input |> Lines.of_channel |> load_lines 
  
let%expect_test "load" =
  let fle = Filename.temp_file "test" ".poly" in
  Out_channel.with_open_text fle (fun out ->
    of_order 3 |> save out
  );
  let polys = In_channel.with_open_text fle load in
  polys |> save stdout
  ; [%expect{|
    A:
    ###

    #
    #
    #


    B:
    ##
    #.

    ##
    .#

    #.
    ##

    .#
    ##


    --- |}]
OCaml

Innovation. Community. Security.