Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let rec skipWhile p xs =
- match xs with
- | [] -> []
- | x :: xr -> if p x then skipWhile p xr else xs
- let has_ng w x history =
- match List.exists (fun e -> x = e) history with
- | true -> true
- | false ->
- let lcands = [x-1 .. -1 .. 0]
- let rcands = [x+1 .. 1 .. w]
- let has_ng' es = Seq.zip history es |> Seq.exists (fun (e0,e1) -> e0 = e1)
- has_ng' lcands || has_ng' rcands
- let solve w h =
- let allxs = List.init w id
- let rec add_line y xs tmp =
- if y >= h
- then
- seq {yield tmp}
- else
- match skipWhile (fun x -> has_ng w x tmp) xs with
- | x :: xr -> seq {yield! add_line (y+1) allxs (x :: tmp);
- yield! add_line y xr tmp}
- | [] -> Seq.empty
- add_line 0 allxs []
- // visualize :: [int] -> string
- let draw size n i =
- match i with
- | _ when i = n -> Some('*', i+1)
- | _ when i > size -> None
- | _ -> Some('-',i+1)
- let seq_to_string s = System.String (Seq.toArray s)
- let visualize board =
- let draw_line = board |> Seq.length |> draw
- let generate_line n = 0 |> Seq.unfold (draw_line n) |> seq_to_string in
- board |> Seq.map generate_line |> String.concat "\n"
- (*
- board = [0;1;2]
- rendering:
- * - -
- - * -
- - - *
- borad = [1;2;0]
- - * -
- - - *
- * - -
- *)
- [<EntryPoint>]
- let main args =
- solve 8 8
- |> Seq.map visualize
- |> printf "%A"
- 0
Add Comment
Please, Sign In to add comment