Guest User

Untitled

a guest
Oct 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. let rec skipWhile p xs =
  2. match xs with
  3. | [] -> []
  4. | x :: xr -> if p x then skipWhile p xr else xs
  5.  
  6. let has_ng w x history =
  7. match List.exists (fun e -> x = e) history with
  8. | true -> true
  9. | false ->
  10. let lcands = [x-1 .. -1 .. 0]
  11. let rcands = [x+1 .. 1 .. w]
  12. let has_ng' es = Seq.zip history es |> Seq.exists (fun (e0,e1) -> e0 = e1)
  13. has_ng' lcands || has_ng' rcands
  14.  
  15. let solve w h =
  16. let allxs = List.init w id
  17. let rec add_line y xs tmp =
  18. if y >= h
  19. then
  20. seq {yield tmp}
  21. else
  22. match skipWhile (fun x -> has_ng w x tmp) xs with
  23. | x :: xr -> seq {yield! add_line (y+1) allxs (x :: tmp);
  24. yield! add_line y xr tmp}
  25. | [] -> Seq.empty
  26. add_line 0 allxs []
  27.  
  28.  
  29.  
  30. // visualize :: [int] -> string
  31. let draw size n i =
  32. match i with
  33. | _ when i = n -> Some('*', i+1)
  34. | _ when i > size -> None
  35. | _ -> Some('-',i+1)
  36.  
  37. let seq_to_string s = System.String (Seq.toArray s)
  38.  
  39. let visualize board =
  40. let draw_line = board |> Seq.length |> draw
  41. let generate_line n = 0 |> Seq.unfold (draw_line n) |> seq_to_string in
  42. board |> Seq.map generate_line |> String.concat "\n"
  43.  
  44. (*
  45. board = [0;1;2]
  46.  
  47. rendering:
  48. * - -
  49. - * -
  50. - - *
  51.  
  52. borad = [1;2;0]
  53. - * -
  54. - - *
  55. * - -
  56. *)
  57.  
  58. [<EntryPoint>]
  59. let main args =
  60. solve 8 8
  61. |> Seq.map visualize
  62. |> printf "%A"
  63. 0
Add Comment
Please, Sign In to add comment