Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- type Network = {id:char; hosts:int; infected:int }
- type Link = { n1:char; n2:char; capacity:int }
- type Rate = {S:float ; I:float; R:float}
- //Natural number
- let (|Nat|_|) str =
- match System.Int32.TryParse(str) with
- | (true,int) when int > 0 -> Some(int)
- | _ -> None
- //Integer greater then 0
- let (|ZeroNAT|_|) str =
- match System.Int32.TryParse(str) with
- | (true,int) when int >= 0 -> Some(int)
- | _ -> None
- let (|Float|_|) str =
- match Double.TryParse(str) with
- | (true,v) when v > 0.0 -> Some(v)
- | _ -> None
- let (|Char|_|) (str:string) =
- match str |> String.length with
- | 1 -> Some (str |> Seq.head)
- | _ -> None
- type IOWorkFlow() =
- member this.Bind(x, f) = Result.bind f x
- member this.Return(x) =
- Ok x
- let createdWorkflow = new IOWorkFlow()
- let getNaturalNumber () =
- match Console.ReadLine() with
- | Nat v -> Ok v
- | _ -> Error "invalid number, must be greater then 0"
- let tryRead() =
- match Console.ReadLine() with
- | null -> Error "not Expecting empty input"
- | v -> Ok v
- let maybeNetwork (str:string) =
- match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
- | [|Char c; Nat h; ZeroNAT i |] -> Ok {id = c; hosts = h; infected = i }
- | _ -> Error "Invalid network"
- let maybeLink (str:string) =
- match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
- | [|Char c1; Char c2; Nat cap |] -> Ok { n1=c1; n2=c2; capacity= cap}
- | _ -> Error "Invalid Link networkID1(char) networkID2(char) capacity(int greater then 0)"
- let maybeRate (str:string) =
- match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
- | [|Float spread; Float infected; Float rate |] -> Ok {S = spread; I = infected; R = rate;}
- | _ -> Error "Invalid Rate spread(float) infected(float) rate(float)"
- let getNetworks n =
- //Some kind of sequence or list to replace this?
- let rec tryNewnetwork lis =
- printfn "enter network ID(char) hosts(integer greater then 0 ) infected(integer greater or equal to 0)"
- tryRead()
- |> Result.bind(maybeNetwork)
- |> Result.bind(fun x -> if not ( lis |> List.exists(fun y -> x.id = y.id)) then Ok x else Error "A network with that ID already exists")
- |>function
- | Ok newNet -> match (newNet::lis) with
- | v when List.length v = n -> v
- | v -> tryNewnetwork v
- | Error e -> printfn "%s" e
- tryNewnetwork lis
- tryNewnetwork []
- let identicalLink l1 l2 =
- ((l1.n1 = l2.n1 && l1.n2 = l2.n2 )||(l1.n1 = l2.n2 && l1.n2 = l2.n1))
- let linkHasNetworks networkLis l1 =
- List.exists(fun y -> l1.n1 = y.id) networkLis && List.exists(fun y -> l1.n2 = y.id) networkLis
- let getLinks n networkLis =
- //Some kind of sequence or list to replace this?
- let rec trylistofLinks lis =
- printfn "enter Link networkID1(char) networkID2(char) capacity(int greater then 0)"
- tryRead()
- |> Result.bind(maybeLink)
- |> Result.bind(fun x -> match x.n1 = x.n2 with
- | false -> Ok x
- | true -> Error "can't connect to the same network")
- |> Result.bind(fun x -> if not ( lis |> List.exists(fun y -> identicalLink x y )) then Ok x else Error "A Link connecting these two networks already exists")
- |> Result.bind(fun x -> match linkHasNetworks networkLis x with
- | true -> Ok x
- | false -> Error "Both networks need to exist")
- |>function
- | Ok newNet -> match (newNet::lis) |> List.length with
- | v when v = n -> lis
- | _ -> trylistofLinks lis
- | Error e -> printfn "%s" e
- trylistofLinks lis
- trylistofLinks []
- let getRate () =
- printfn "Enter rate spread(float) infected(float) rate(float)"
- tryRead() |>
- Result.bind maybeRate
- let simulate networks links rate packetSize = ignore
- let inputAll() =
- createdWorkflow
- {
- printfn "Enter Number of networks"
- let! networks = getNaturalNumber()
- |> Result.map getNetworks
- printfn "Enter Number of Links"
- let! links = getNaturalNumber()
- |> Result.map (fun n -> getLinks n networks)
- let! rate = getRate()
- printfn "Enter packet size"
- let! packetSize = getNaturalNumber()
- return simulate networks links rate packetSize
- }
- [<EntryPoint>]
- let main argv =
- inputAll()
- |>function
- | Ok v -> printfn "yay"
- | Error e -> printfn "%s" e
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement