fork download
  1. let perms xs =
  2. let distrib e L =
  3. let rec aux pre post =
  4. seq {
  5. match post with
  6. | [] -> yield (L @ [e])
  7. | h::t -> yield (List.rev pre @ [e] @ post)
  8. yield! aux (h::pre) t
  9. }
  10. aux [] L
  11. let rec loop = function
  12. | [] -> Seq.singleton []
  13. | h::t -> Seq.collect (distrib h) (loop t)
  14. loop xs
  15.  
  16.  
  17. let solve xs =
  18. let dxs = dict xs
  19. let yxs = seq {
  20. for var in perms xs do
  21. let row = var
  22. |> List.collect (fun (n,xs) -> xs |> List.mapi (fun i e -> (n, i + 1, i + 2, e)))
  23. |> List.toArray
  24. let fname (a,x,_,_) = sprintf "%s%A" a x
  25. let lname (a,_,x,_) = sprintf "%s%A" a x
  26.  
  27. yield! seq {
  28. for i = 0 to row.Length - 2 do
  29. for k = i to row.Length - 1 do
  30. let pins = row.[i..k]
  31. |> Seq.map (fun (a,_,_,_) -> a)
  32. |> Seq.distinct
  33. |> Seq.pairwise
  34. |> Seq.map (fun (a, b) -> sprintf "%s%A-%s%A" a (dxs.[a].Length + 1) b 1 )
  35. |> String.concat ", "
  36. yield (fname row.[i]), (lname row.[k]), (row.[i..k] |> Seq.sumBy (fun (_,_,_,e)->e)), pins
  37. }
  38. }
  39.  
  40. let xs = yxs
  41. |> Seq.distinct
  42. |> Seq.toArray
  43. |> Array.sortBy (fun (_,_,a,_) -> a)
  44. printfn " outputs = V\t jumper"
  45. for (a,b,c,d) in xs do
  46. printfn " %5s..%-5s = %A \t%s" a b c d
  47.  
  48.  
  49.  
  50. do
  51. let xs = [
  52. ("xx", [4.4; 7.7; 3.85])
  53. ("xxx", [1.1; 0.55])
  54. ]
  55. solve xs
Success #stdin #stdout 0.26s 13296KB
stdin
Standard input is empty
stdout
    outputs   =   V	 jumper
  xxx2..xxx3  = 0.55 	
  xxx1..xxx2  = 1.1 	
  xxx1..xxx3  = 1.65 	
   xx3..xx4   = 3.85 	
   xx1..xx2   = 4.4 	
  xxx2..xx2   = 4.95 	xxx3-xx1
   xx3..xxx2  = 4.95 	xx4-xxx1
   xx3..xxx3  = 5.5 	xx4-xxx1
  xxx1..xx2   = 6.05 	xxx3-xx1
   xx2..xx3   = 7.7 	
   xx2..xx4   = 11.55 	
   xx1..xx3   = 12.1 	
   xx2..xxx2  = 12.65 	xx4-xxx1
  xxx2..xx3   = 12.65 	xxx3-xx1
   xx2..xxx3  = 13.2 	xx4-xxx1
  xxx1..xx3   = 13.75 	xxx3-xx1
   xx1..xx4   = 15.95 	
  xxx2..xx4   = 16.5 	xxx3-xx1
   xx1..xxx2  = 17.05 	xx4-xxx1
  xxx1..xx4   = 17.6 	xxx3-xx1
   xx1..xxx3  = 17.6 	xx4-xxx1