Skip to content

Instantly share code, notes, and snippets.

@DedSec256
Last active June 1, 2022 02:50
Show Gist options
  • Select an option

  • Save DedSec256/92900e62b063f1991954a8554a57c8dc to your computer and use it in GitHub Desktop.

Select an option

Save DedSec256/92900e62b063f1991954a8554a57c8dc to your computer and use it in GitHub Desktop.
Local network virus
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