fork download
  1. module Y =
  2. let permutation xxs =
  3. let rec ins (acc : _ list list) (x : _) (hh : _ list) (xs : _ list) =
  4. match xs with
  5. | [] -> (hh @ [x]) :: acc
  6. | h :: t -> ins ((hh @ (x :: xs)) :: acc) x (h :: hh) t
  7. let rec loop (acc : _ list list) xs =
  8. match xs with
  9. | x :: t ->
  10. let acc = List.collect (fun a -> ins [] x [] a) acc
  11. loop acc t
  12. | _ -> acc
  13. loop [[]] xxs
  14.  
  15. let not_ontop x (arr : _[]) = arr.[Array.length arr - 1] <> x
  16. let not_onbottom x (arr : _[]) = arr.[0] <> x
  17. let level x (arr : _[]) = Array.findIndex ((=)x) arr
  18. let not_near a b (arr : _[]) = abs (level a arr - level b arr) <> 1
  19. let higher_than a b (arr : _[]) = level a arr < level b arr
  20.  
  21. let solver rules mens =
  22. for house' in permutation mens do
  23. let house = List.toArray house'
  24. if List.forall (fun rule -> rule house) rules
  25. then printfn "%A" house
  26.  
  27. open Y
  28.  
  29. type Someone = | Baker | Cooper | Fletcher | Miller | Smith
  30.  
  31. [<EntryPoint>]
  32. let main argv =
  33. let mens = [ Baker; Cooper; Fletcher; Miller; Smith ]
  34. let rules = [ Baker |> not_ontop
  35. Cooper |> not_onbottom
  36. Fletcher |> not_ontop
  37. Fletcher |> not_onbottom
  38. Miller |> higher_than Cooper
  39. Smith |> not_near Fletcher
  40. Fletcher |> not_near Cooper
  41. ]
  42. Y.solver rules mens
  43. 0
Success #stdin #stdout 0.22s 12896KB
stdin
Standard input is empty
stdout
[|Smith; Cooper; Baker; Fletcher; Miller|]