0xDEADBEEF

RSS odkazy english edition

covid-killers.scala

12. 6. 2021 #kód
// https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19/nakazeni-vyleceni-umrti-testy.csv
val file = "nakazeni-vyleceni-umrti-testy.csv"
val lines = io.Source.fromFile(file).getLines.toVector.drop(1)
val dailyCummulativeStats = lines.map { line =>
  val Array(date, infected, _, deaths, _*) = line.split(",")
  (date, infected.toInt, deaths.toInt)
}

val weeklyCummulativeStats =
  dailyCummulativeStats
    .grouped(7)
    .map { ls => ls.last }
    .toVector

val weeklyStats =
  weeklyCummulativeStats.drop(1).zip(weeklyCummulativeStats)
    .map { case ((date, inf, dth), (_, prevInf, prevDth)) =>
      (date, inf - prevInf, dth - prevDth)
    }

class Person {
  var infectedBy: Person = null
  var died = false
  var killed = 0
}

def pickOne[T](xs: Seq[T]) = xs(util.Random.nextInt(xs.length))

var population = Vector[Vector[Person]]()

for (((_, infected, died), i) <- weeklyStats.zipWithIndex) {

  // newly infected people
  val people = Vector.fill(infected)(new Person)

  // they were infected from last week's infectees
  if (i > 0 && population(i-1).nonEmpty) {
    val source = population(i-1)
    people.foreach { _.infectedBy = pickOne(source) }
  }

  population = population :+ people

  // people who died were infected 2 weeks ago
  if (i >= 2) {
    val moribunds = population(i-2)
    util.Random.shuffle(moribunds).take(died).foreach { _.died = true }
  }
}


for (people <- population; person <- people if person.died) {
  var p = person.infectedBy

  while (p != null) {
    p.killed += 1
    p = p.infectedBy
  }

  person
}

var total = 0.0
var killers = 0

for ((people, i) <- population.zipWithIndex) {
  val n = people.size
  val k = people.map(_.killed).sum
  val a = if (n == 0) 0.0 else k / n.toDouble

  total += k
  killers += people.count(_.killed > 0)

  println(""+weeklyStats(i)._1+" "+a)
}

println(total / population.flatten.size)
println(killers)
píše k47 (@kaja47, k47)