package knights_tour
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/puzzle.ml.html
Source file puzzle.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
open Knights_tour type t = { (** pieces remaining to be placed *) pieces : Polyomino.t list; (** board upon which to place the pieces *) board : Board.t; } let classic = { pieces = Polyomino.of_order 5; board = Board.classic; } let classic_no_symmetric_solutions = { classic with pieces = classic.pieces |> Polyomino.pin_symmetry } let lowest_point board = Board.vacant board |> PointSet.min_elt type placement = { piece: Polyomino.t; points: PointSet.t; } let move_to_target target points = let open Searchspace in Searchspace.of_seq (PointSet.to_seq points) |-> (fun point -> PointSet.translate (Point.diff target point) points) let place_one piece board target = let open Searchspace in Polyomino.variants piece |> Searchspace.of_list |=> move_to_target target |?> (fun points -> points |> PointSet.for_all (fun point -> Board.(get board point = Vacant)) ) |-> (fun points -> {points;piece}) let smallest measure xs = let keep_smallest (sx,x) (sy,y) = if Int.compare sx sy <= 0 then (sx, x) else (sy, y) in match List.map (fun x -> (measure x, x)) xs with | [] -> failwith "An empty list doesn't have a smallest element" | fst::rst -> List.fold_left keep_smallest fst rst |> snd let%expect_test "smallest string" = let cases = [ ["Hello"; "World"; "Very long"; "a"]; ["The"; "Quick"; "Brown fox"; "jumps"]; ["Short"; "in"; "the"; "middle"]; ["all"; "the"; "sam"] ] in cases |> List.iter (fun words -> words |> List.iter (Printf.printf "%s; "); smallest String.length words |> Printf.printf " => %s\n" ) ;[%expect{| Hello; World; Very long; a; => a The; Quick; Brown fox; jumps; => The Short; in; the; middle; => in all; the; sam; => all |}] (** Finds all valid placements for the most constrained target.*) let most_constrained_point {pieces; board} targets = let open Searchspace in let placements_per_target = targets |> PointSet.to_seq |> Seq.map (fun target -> pieces |> Searchspace.of_list |=> (fun piece -> place_one piece board target) |> Searchspace.to_seq |> List.of_seq ) |> List.of_seq in smallest List.length placements_per_target |> of_list let%expect_test "most_constrained_point" = let board = Board.of_string " ######## ######## ######## ###..### ###..### ######## #######. ######## " in let puzzle = {board; pieces=Polyomino.of_order 5} in let vacancies = Board.vacant board in let most_constrained = most_constrained_point puzzle vacancies in most_constrained |> Searchspace.to_seq |> Seq.iter (fun {piece;points} -> Board.put puzzle.board points piece |> Board.to_string |> Printf.printf "%s\n" ) ;[%expect{| ........ ........ ........ ...##... ...##... ........ .......# ...AAAAA ........ ........ ........ ...##... ...##... ........ ....B..# ....BBBB ........ ........ ........ ...##... ...##.B. ......B. ......B# ......BB ........ ........ ........ ...##... ...##... ........ .....C.# ....CCCC ........ ........ ........ ...##... ...##... ........ ......C# ....CCCC ........ ........ ........ ...##... ...##... ........ .....DD# .....DDD ........ ........ ........ ...##... ...##... ......EE ......E# ......EE ........ ........ ........ ...##... ...##... .....F.. .....F.# .....FFF ........ ........ ........ ...##... ...##... ......G. ......G# .....GGG ........ ........ ........ ...##... ...##... ........ ....HHH# ......HH ........ ........ ........ ...##... ...##... ........ ....HH.# .....HHH ........ ........ ........ ...##... ...##... ......I. .....II# ......II ........ ........ ........ ...##... ...##... .....J.. .....JJ# ......JJ ........ ........ ........ ...##... ...##... .....KK. ......K# ......KK |}] let rec solve_aux ~report_progress puzzle targets = let open Searchspace in if PointSet.is_empty targets then ( report_progress "Solved" puzzle; return puzzle.board ) else most_constrained_point puzzle targets |=> (fun {piece; points} -> let puzzle = { board=Board.put puzzle.board points piece; pieces=List.filter (fun p -> Polyomino.compare p piece <> 0) puzzle.pieces; } in report_progress "Placed a piece" puzzle; let targets = PointSet.union targets (PointSet.adjacent points) |> PointSet.filter (fun pt -> (Board.get puzzle.board pt) = Vacant) in solve_aux ~report_progress puzzle targets ) let solve ?(report_progress = fun _ _ -> ()) puzzle = solve_aux ~report_progress puzzle (PointSet.singleton (lowest_point puzzle.board)) let%expect_test "Placements of a polyomino on a target" = let puzzle = classic in let pentos = puzzle.pieces in let piece = List.hd pentos in let board = puzzle.board in let target = Point.{x=1;y=2} in Printf.printf "===============\nPlacing @ (%d, %d):\n%s" target.x target.y (Polyomino.to_string piece); place_one piece board target |> Searchspace.to_seq |> Seq.iter (fun placement -> Board.put board placement.points placement.piece |> Board.to_string |> Printf.printf "\n%s" ) ; [%expect{| =============== Placing @ (1, 2): ##### ........ ........ .AAAAA.. ...##... ...##... ........ ........ ........ ........ ........ AAAAA... ...##... ...##... ........ ........ ........ ........ ........ .A...... .A.##... .A.##... .A...... .A...... ........ ........ .A...... .A...... .A.##... .A.##... .A...... ........ ........ .A...... .A...... .A...... .A.##... .A.##... ........ ........ ........ |}] let%expect_test "Placements of all tetro-minos @(0,0)" = let pentos = Polyomino.of_order 4 in let board = Board.classic in let target = Point.{x=0;y=0} in pentos |> List.iter (fun piece -> Printf.printf "===============\nPlacing @ (%d, %d):\n%s" target.x target.y (Polyomino.to_string piece); place_one piece board target |> Searchspace.to_seq |> Seq.iter (fun placement -> Board.put board placement.points placement.piece |> Board.to_string |> Printf.printf "\n%s" ) ) ; [%expect{| =============== Placing @ (0, 0): #### AAAA.... ........ ........ ...##... ...##... ........ ........ ........ A....... A....... A....... A..##... ...##... ........ ........ ........ =============== Placing @ (0, 0): ### #.. BBB..... B....... ........ ...##... ...##... ........ ........ ........ BBB..... ..B..... ........ ...##... ...##... ........ ........ ........ BB...... B....... B....... ...##... ...##... ........ ........ ........ BB...... .B...... .B...... ...##... ...##... ........ ........ ........ B....... BBB..... ........ ...##... ...##... ........ ........ ........ B....... B....... BB...... ...##... ...##... ........ ........ ........ =============== Placing @ (0, 0): ### .#. CCC..... .C...... ........ ...##... ...##... ........ ........ ........ C....... CC...... C....... ...##... ...##... ........ ........ ........ =============== Placing @ (0, 0): ## ## DD...... DD...... ........ ...##... ...##... ........ ........ ........ =============== Placing @ (0, 0): ##. .## EE...... .EE..... ........ ...##... ...##... ........ ........ ........ E....... EE...... .E...... ...##... ...##... ........ ........ ........ |}] let%expect_test "solve classic pentos" = let puzzle = classic in solve puzzle |> Searchspace.to_seq |> Seq.take 4 |> Seq.map Board.to_string |> Seq.iter print_endline ;[%expect{| AAAAAFFF BBBBHHIF BLHHHIIF LLL##KII CLG##KKK CCGGGJJK CEGEDDJJ CEEEDDDJ AAAAAFFF BBBBHHIF BLHHHIIF LLL##KII CLG##KKK CCGGGJJK CEGEJJDD CEEEJDDD AAAAAJJD BBBBJJDD EEEBJGDD EHE##GGG HHL##GKK HLLLIIKF HCLIIKKF CCCCIFFF AAAAADDD BBCCCCDD BGGGCEEE BHG##ELE BHG##LLL HHJKKILF HJJKIIIF JJKKIFFF |}]
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>