package knights_tour

  1. Overview
  2. Docs

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
(** 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
  
let variants ini = 
  let module PointSetSet = Set.Make(struct 
    type nonrec t = t
    let compare = compare 
  end) in
  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 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
    =======================
    ####
    .#.. |}]

    
OCaml

Innovation. Community. Security.