package knights_tour

  1. Overview
  2. No Docs
Solves the 'Knights Tour' and various 'Poyomino' puzzles

Install

dune-project
 Dependency

Authors

Maintainers

Sources

knights_tour-0.0.6.tbz
sha256=770624ae4e35d5a58188a1f25e16730d186df8bc4397827fe9a798ea5ca574e3
sha512=b6c7b2473ddf321afaac6b668f9c6820fb8cc872abc494b69edfdff4101c6b660645837b04801e2917c38dd9f19f79bb0a7b029d59a58ddf1e3a235659bab099

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

Source file pointSet.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
(** A PointSet is a set of points in 2d space. Each point has int coordinates and represents
    a square on a chess-like game board (of unspecified/inifinite dimenions)*)

open Knights_tour

include Set.Make(Point)

let of_string image =
  let lines = 
        String.split_on_char '\n' image
        |> List.map String.trim 
  in 
  let points = ref empty in
  lines |> List.iteri (fun y line ->
    line |> String.iteri (fun x c -> match c with
      | '.' | ' ' -> () 
      | _ -> points := add {x;y} !points
    )
  );
  !points

let min_map selector points =
  fold (fun pt acc -> Int.min (selector pt) acc) points Int.max_int

let max_map selector points =
  fold (fun pt acc -> Int.max (selector pt) acc) points Int.min_int
let min_x = min_map (fun p -> p.x)
let max_x = max_map (fun p -> p.x)
let min_y = min_map (fun p -> p.y)
let max_y = max_map (fun p -> p.y)

let%expect_test "min max" =
  let points = of_string "
      .
      ...#
      ..##
      ...#########
      ..."
  in (
    Format.printf "min_x = %d\n" (min_x points);
    Format.printf "max_x = %d\n" (max_x points);
    Format.printf "min_y = %d\n" (min_y points);
    Format.printf "max_y = %d\n" (max_y points);
  )
  ;[%expect{|
    min_x = 2
    max_x = 11
    min_y = 2
    max_y = 4 |}] 
  
let%expect_test "pointset parsed from string image" =
   of_string (
    ".#     
     ##      
     .##"
  ) 
  |> iter (fun {x;y} -> (Format.printf "(%d, %d)\n" x y))
  ;[%expect{|
    (1, 0)
    (0, 1)
    (1, 1)
    (1, 2)
    (2, 2) |}]
 
let to_string points =
  let image = Buffer.create 60 in
  for y = min_y points to max_y points do 
    for x = min_x points to max_x points do
      Buffer.add_char image (
        if mem {x;y} points then '#' else '.'
      )
    done;
    Buffer.add_char image '\n'
  done;
  Buffer.contents image

let%expect_test "to_string" =
  let points = of_string "
      .
      ...#
      ..##
      ...#########
      ..."
  in print_endline (to_string points)
  ;[%expect{|
    .#........
    ##........
    .######### |}] 

let neighbours Point.{x;y} = Point.[
  { x = x - 1 ; y         };
  { x         ; y = y - 1 };
  { x = x + 1 ; y         };
  { x         ; y = y + 1 }
] |> of_list

let flatmap f points = fold (fun elt acc -> union (f elt) acc) points empty

let adjacent points = 
  let all_neighbours = points |> flatmap neighbours in
  diff all_neighbours points

let%expect_test "adjacent" =
  let input = of_string "
    #
    #
    ##
  " in
  input
  |> adjacent
  |> (fun adjacent -> 
    Printf.printf "org: (%d, %d)\n" (min_x input) (min_y input);
    Printf.printf "adj: (%d, %d)\n" (min_x adjacent) (min_y adjacent);
    Printf.printf "%s\n" (to_string adjacent)
  )
  ;[%expect{|
    org: (0, 1)
    adj: (-1, 0)
    .#..
    #.#.
    #.#.
    #..#
    .##. |}]

let translate (delta : Point.t) = map (fun {x; y} -> {
  x = x + delta.x;
  y = y + delta.y
})

let normalize_translation points =
  let min_x = points |> min_x in
  let min_y = points |> min_y in
  translate {x = -min_x; y = -min_y} points 

let rotate_point Point.{x;y} = Point.{x = -y; y = x}

let mirror_point Point.{x;y} = Point.{y = x; x = y}

let rotate points = map rotate_point points |> normalize_translation
let mirror = map mirror_point

module PointSetSet = Set.Make(struct 
  type nonrec t = t
  let compare = compare 
end)

let variants ini = 
  let open Searchspace in
  (
    int_range 0 1 |=> fun mirror_count ->
    int_range 0 3 |-> fun rot_count -> 
      ini |> normalize_translation
      |> Fun.repeat rot_count rotate 
      |> Fun.repeat mirror_count mirror
  )
  |> Searchspace.to_seq
  |> PointSetSet.of_seq
  |> PointSetSet.to_seq
  |> List.of_seq
  
let variants_with_transform shape =
  let open Searchspace in
  let transforms =
    int_range 0 1 |=> fun mirror_count ->
    int_range 0 3 |-> fun rot_count ->
      fun pts ->
        pts
        |> normalize_translation
        |> Fun.repeat rot_count rotate
        |> Fun.repeat mirror_count mirror
  in
  let seen = ref PointSetSet.empty in
  let results = ref [] in
  transforms |> to_seq |> Seq.iter (fun transform ->
    let transformed = transform shape |> normalize_translation in
    if not (PointSetSet.mem transformed !seen) then begin
      seen := PointSetSet.add transformed !seen;
      results := (transform, transformed) :: !results
    end
  );
  List.rev !results
  
let normalize points = 
  points
  |> variants
  |> List.hd
  
let%expect_test "variants assymetric" =
  of_string
    ".# 
     ##
     .#
     .#"
  |> variants
  |> List.map to_string
  |> List.iter (Format.printf "-------------\n%s")
  ;[%expect{|
    -------------
    ####
    .#..
    -------------
    ####
    ..#.
    -------------
    #.
    ##
    #.
    #.
    -------------
    #.
    #.
    ##
    #.
    -------------
    .#..
    ####
    -------------
    .#
    ##
    .#
    .#
    -------------
    .#
    .#
    ##
    .#
    -------------
    ..#.
    #### |}]

let%expect_test "variants symmetric" = 
  of_string
    ".#
     ###
     .#"
  |> variants
  |> List.map to_string
  |> List.iter (Format.printf "-------------\n%s")
  ;[%expect{|
    -------------
    .#.
    ###
    .#. |}]

  
let%expect_test "normalized rep of variants are equal to one another" =
  let open Printf in
  let vars = of_string
    ".# 
     ##
     .#
     .#"
    |> variants
    |> Array.of_list
  in
    vars |> Array.iteri (fun i1 v1 ->
      printf "%d: " i1;
      vars |> Array.iter (fun v2 ->
        printf "%b " ((compare (normalize v1) (normalize v2)) = 0)
      );
      printf "\n"
    ) ; [%expect{|
      0: true true true true true true true true
      1: true true true true true true true true
      2: true true true true true true true true
      3: true true true true true true true true
      4: true true true true true true true true
      5: true true true true true true true true
      6: true true true true true true true true
      7: true true true true true true true true |}]

let%expect_test "rotate and normalize translation" =
  let points = of_string (
    ".#     
     ##      
     .#
     .#"
  ) in
  for i = 0 to 3 do
    Fun.repeat i rotate points
    |> (fun points -> 
      Printf.printf "=======================\n%!";
      Printf.printf "min_x : %d   min_y : %d\n%!" (min_x points) (min_y points);
      Printf.printf "=======================\n%!";
      Printf.printf "%s\n%!" (to_string points)
    )
  done 
  ; [%expect{|
    =======================
    min_x : 0   min_y : 0
    =======================
    .#
    ##
    .#
    .#

    =======================
    min_x : 0   min_y : 0
    =======================
    ..#.
    ####

    =======================
    min_x : 0   min_y : 0
    =======================
    #.
    #.
    ##
    #.

    =======================
    min_x : 0   min_y : 0
    =======================
    ####
    .#.. |}]

let%expect_test "variants_with_transform" =
  let input_shapes = List.map of_string [
      "##
       ##"
   ;
      "##
       ##
       ##
       ##"
   ;
      "#.
       ##"
    ;
      "#
       ##"
    ;
      "#
       #
       ##"
  ] in
  let test_shape = of_string (
    ".#     
     ##      
     .#
     .#"
  ) in
  input_shapes |> List.iter (fun shape ->
    Printf.printf "Shape:\n%s\n" (to_string shape);
  let syms = variants_with_transform shape in
    Printf.printf "Symmetries: %d\n" (List.length syms);
    List.iteri (fun i (transform, b) -> (
      Printf.printf "Symmetry %d:\n" i;
      Printf.printf "%s\n" (to_string b);
      Printf.printf "Transformed shape:\n";
      Printf.printf "%s\n" (test_shape |> transform |> to_string);
      Printf.printf "---\n"
    )) syms;
    Printf.printf "========================\n"
  ); 
  [%expect{|
    Shape:
    ##
    ##

    Symmetries: 1
    Symmetry 0:
    ##
    ##

    Transformed shape:
    .#
    ##
    .#
    .#

    ---
    ========================
    Shape:
    ##
    ##
    ##
    ##

    Symmetries: 2
    Symmetry 0:
    ##
    ##
    ##
    ##

    Transformed shape:
    .#
    ##
    .#
    .#

    ---
    Symmetry 1:
    ####
    ####

    Transformed shape:
    ..#.
    ####

    ---
    ========================
    Shape:
    #.
    ##

    Symmetries: 4
    Symmetry 0:
    #.
    ##

    Transformed shape:
    .#
    ##
    .#
    .#

    ---
    Symmetry 1:
    ##
    #.

    Transformed shape:
    ..#.
    ####

    ---
    Symmetry 2:
    ##
    .#

    Transformed shape:
    #.
    #.
    ##
    #.

    ---
    Symmetry 3:
    .#
    ##

    Transformed shape:
    ####
    .#..

    ---
    ========================
    Shape:
    #.
    ##

    Symmetries: 4
    Symmetry 0:
    #.
    ##

    Transformed shape:
    .#
    ##
    .#
    .#

    ---
    Symmetry 1:
    ##
    #.

    Transformed shape:
    ..#.
    ####

    ---
    Symmetry 2:
    ##
    .#

    Transformed shape:
    #.
    #.
    ##
    #.

    ---
    Symmetry 3:
    .#
    ##

    Transformed shape:
    ####
    .#..

    ---
    ========================
    Shape:
    #.
    #.
    ##

    Symmetries: 8
    Symmetry 0:
    #.
    #.
    ##

    Transformed shape:
    .#
    ##
    .#
    .#

    ---
    Symmetry 1:
    ###
    #..

    Transformed shape:
    ..#.
    ####

    ---
    Symmetry 2:
    ##
    .#
    .#

    Transformed shape:
    #.
    #.
    ##
    #.

    ---
    Symmetry 3:
    ..#
    ###

    Transformed shape:
    ####
    .#..

    ---
    Symmetry 4:
    ###
    ..#

    Transformed shape:
    .#..
    ####

    ---
    Symmetry 5:
    ##
    #.
    #.

    Transformed shape:
    .#
    .#
    ##
    .#

    ---
    Symmetry 6:
    #..
    ###

    Transformed shape:
    ####
    ..#.

    ---
    Symmetry 7:
    .#
    .#
    ##

    Transformed shape:
    #.
    ##
    #.
    #.

    ---
    ========================
    |}]