Last active
June 1, 2022 02:50
-
-
Save DedSec256/92900e62b063f1991954a8554a57c8dc to your computer and use it in GitHub Desktop.
Local network virus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Virus(firstInfected: Computer[], infectionChance: OS -> float, random: unit -> float) = | |
| let infectedComputers = HashSet(firstInfected) | |
| let getNeighbours computer: Computer[] = [||] // TODO: ... | |
| let calculateInfectionCandidates lastInfected = | |
| lastInfected | |
| |> Seq.map getNeighbours | |
| |> Seq.concat | |
| |> Seq.distinct | |
| |> Seq.filter (not << infectedComputers.Contains) | |
| |> Seq.filter (fun x -> infectionChance x.OperationSystem > 0) | |
| let mutable infectionCandidates = calculateInfectionCandidates infectedComputers | |
| member x.InfectedComputers = infectedComputers | |
| member x.AbleToInfect = not (Seq.isEmpty infectionCandidates) | |
| member x.SpreadInfection() = | |
| let newInfected = List() | |
| for computer in infectionCandidates do | |
| if random() < infectionChance computer.OperationSystem then | |
| newInfected.Add(computer) | |
| infectedComputers.UnionWith(newInfected) | |
| infectionCandidates <- calculateInfectionCandidates newInfected | |
| type PlagueInc(viruses: Virus[]) = | |
| let rec play viruses = | |
| let viruses = viruses |> Seq.filter (fun (v: Virus) -> v.AbleToInfect) | |
| if Seq.isEmpty viruses then () else | |
| for virus in viruses do virus.SpreadInfection() | |
| play viruses | |
| member x.Play() = play viruses | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment