Advertisement
Guest User

Worm War 2

a guest
Aug 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.34 KB | None | 0 0
  1. open System
  2.  
  3. type Network = {id:char; hosts:int; infected:int  }
  4.  
  5. type Link = { n1:char; n2:char; capacity:int }
  6.  
  7. type Rate = {S:float ; I:float; R:float}
  8.  
  9. //Natural number
  10. let (|Nat|_|) str =
  11.    match System.Int32.TryParse(str) with
  12.    | (true,int) when int > 0 -> Some(int)
  13.    | _ -> None
  14. //Integer greater then 0
  15. let (|ZeroNAT|_|) str =
  16.    match System.Int32.TryParse(str) with
  17.    | (true,int) when int >= 0 -> Some(int)
  18.    | _ -> None
  19.  
  20. let (|Float|_|) str =
  21.     match Double.TryParse(str) with
  22.     | (true,v)  when v > 0.0 ->  Some(v)
  23.     | _ -> None
  24.  
  25. let (|Char|_|) (str:string) =
  26.     match str |> String.length with
  27.     | 1 -> Some (str |> Seq.head)
  28.     | _ -> None
  29.  
  30. let getNaturalNumber () =
  31.     match Console.ReadLine() with
  32.     | Nat v -> Ok v
  33.     | _ -> Error "invalid number, must be greater then 0"
  34.  
  35. let tryRead() =
  36.     match Console.ReadLine() with
  37.     | null -> Error "not Expecting empty input"
  38.     | v -> Ok v
  39. let maybeNetwork (str:string) =
  40.     match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
  41.     | [|Char c; Nat h; ZeroNAT i |] -> Ok {id = c; hosts = h; infected = i }
  42.     | _ -> Error "Invalid network"
  43.  
  44. let maybeLink (str:string) =
  45.    match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
  46.    | [|Char c1; Char c2; Nat cap  |] -> Ok { n1=c1; n2=c2; capacity= cap}
  47.    | _ -> Error "Invalid Link"
  48.  
  49. let maybeRate (str:string) =
  50.     match str.Split(' ', StringSplitOptions.RemoveEmptyEntries) with
  51.     | [|Float spread; Float infected; Float rate |] -> Ok {S = spread; I = infected; R = rate;}
  52.     | _ -> Error "Invalid Rate spread(float) infected(float) rate(float)"
  53.  
  54. let getNetworks n =
  55.     //Some kind of sequence or list to replace this?
  56.    let rec tryNewnetwork lis =
  57.  
  58.        printfn "enter network ID(char) hosts(integer greater then 0 ) infected(integer greater or equal to 0)"
  59.        tryRead()
  60.        |> Result.bind(maybeNetwork)
  61.        |> 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")
  62.        |>function
  63.        | Ok  newNet -> match (newNet::lis) |> List.length with
  64.                         | v when v = n -> lis
  65.                         | _ -> tryNewnetwork lis
  66.        | Error e ->  printfn "%s" e
  67.                      tryNewnetwork lis
  68.    tryNewnetwork []
  69.  
  70. let identicalLink l1 l2 =
  71.     ((l1.n1 = l2.n1 && l1.n2 = l2.n2 )||(l1.n1 = l2.n2 && l1.n2 = l2.n1))
  72.  
  73. let linkHasNetworks networkLis l1  =
  74.     List.exists(fun y -> l1.n1 = y.id) networkLis && List.exists(fun y -> l1.n2 = y.id) networkLis
  75.  
  76. let getLinks n networkLis =
  77.      //Some kind of sequence or list to replace this?
  78.    let rec trylistofLinks lis =
  79.        tryRead()
  80.        |> Result.bind(maybeLink)
  81.        |> Result.bind(fun x -> match x.n1 = x.n2 with
  82.                               | false -> Ok x
  83.                               | true -> Error "can't connect to the same network")
  84.        |> 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")
  85.        |> Result.bind(fun x ->  match linkHasNetworks networkLis x with
  86.                                | true -> Ok x
  87.                                | false -> Error "Both networks need to exist")
  88.        |>function
  89.        | Ok  newNet -> match (newNet::lis) |> List.length with
  90.                         | v when v = n -> lis
  91.                         | _ -> trylistofLinks lis
  92.        | Error e ->  printfn "%s" e
  93.                      trylistofLinks lis
  94.    trylistofLinks []
  95.  
  96. let getRate () =
  97.     printfn "Enter rate spread(float) infected(float) rate(float)"
  98.     tryRead() |>
  99.     Result.bind maybeRate
  100.  
  101. let simulate networks links rate packetSize = ignore
  102.  
  103. [<EntryPoint>]
  104. let main argv =
  105.     printfn "Enter Number of networks"
  106.     let networks = getNaturalNumber()
  107.                    |> Result.map getNetworks
  108.     printfn "Enter Number of Links"
  109.     let links = getNaturalNumber()
  110.                 |> Result.map getLinks
  111.                 |> Result.bind (fun netToLinks -> networks |> Result.map(fun net -> netToLinks net ))
  112.     let rate = getRate()
  113.  
  114.     printfn "Enter packet size"
  115.     getNaturalNumber()
  116.  
  117.     |> function //output
  118.     | Ok result -> result
  119.                    |> printfn "%A"
  120.     | Error e -> printfn "%s" e
  121.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement