<commit_before>import scala.collection.mutable.{Set,Map}
import scala.io.StdIn

/**
  * Auto-generated code below aims at helping you parse
  * the standard input according to the problem statement.
  **/
object Solution extends App {
  val n = StdIn.readInt // the number of adjacency relations
  val edges = Set[Edge]()
  val nodes = Set[Int]()
  for (i <- 0 until n) {
    // xi: the ID of a person which is adjacent to yi
    // yi: the ID of a person which is adjacent to xi
    val Array(id1, id2) = for (id <- StdIn.readLine split " ") yield id.toInt
    edges += Edge(id1, id2) += Edge(id2, id1)
    nodes += id1 += id2
  }

  //Console.err.println(edges)

  val graph = Graph(Node.constructNodesList(nodes, edges), edges)

  //Console.err.println(graph)

  // Write an action using println
  // To debug: Console.err.println("Debug messages...")

  // The minimal amount of steps required to completely propagate the advertisement
  println(Graph.computeMinimalTime(graph).toString)
}

case class Edge(source: Int, destination: Int)

case class Node(content: Int, adjacency: Set[Int])

case class Graph(nodes: Set[Node], edges: Set[Edge]) {
  val cache:Map[Int,Node] = Map[Int,Node]()
  def getNodeFromId(id: Int): Node = {
    if(!cache.contains(id)) {
      cache.put(id,this.nodes.filter(_.content == id).head)
    }
    cache(id)
  }
}

object Node {
  def constructNodesList(nodes: Set[Int], edges: Set[Edge]): Set[Node] = {
    nodes.map(n => Node(n, edges.filter(e => e.source == n).map(e => e.destination)))
  }
}

object Graph {
  def computeAdjacencyReach(graph: Graph, node: Node): Int = {
    var setsOfAllNode = Set[Node](node)
    var number = 0
    var currentSet = node.adjacency.map(graph.getNodeFromId(_))
    while (!currentSet.isEmpty) {
      setsOfAllNode ++= currentSet
      number += 1
      currentSet = currentSet.flatMap(n => n.adjacency.map(graph.getNodeFromId(_))) -- setsOfAllNode
    }
    number
  }

  def computeMinimalTime(graph: Graph): Int = {
    graph.nodes.map(computeAdjacencyReach(graph, _)).min
  }
}<commit_msg>WIP - Travail sur une solution en Scala pour résoudre le puzzle Medium "Teads Contest"<commit_after>import scala.collection.mutable.{Set,Map}
import scala.io.StdIn

/**
  * Auto-generated code below aims at helping you parse
  * the standard input according to the problem statement.
  **/
object Solution extends App {
  val n = StdIn.readInt // the number of adjacency relations
  val graph = Graph()
  for (i <- 0 until n) {
    // xi: the ID of a person which is adjacent to yi
    // yi: the ID of a person which is adjacent to xi
    val Array(id1, id2) = for (id <- StdIn.readLine split " ") yield id.toInt
    graph.addEdges(id1,id2)
  }

  //Console.err.println(graph)

  // Write an action using println
  // To debug: Console.err.println("Debug messages...")

  // The minimal amount of steps required to completely propagate the advertisement
  println(graph.computeMinimalTime().toString)
}

case class Graph() {

  private val nodes = Set[Int]()
  private val nodesById:Map[Int,Set[Int]] = Map[Int,Set[Int]]()
  def addEdges(source:Int,destination:Int):Unit = {
    def addOrUpdateNode(source:Int,destination:Int):Unit = {
      if(!nodesById.contains(source)) {
        nodesById.put(source,Set[Int](destination))
      }
      else {
        nodesById.get(source).get += destination
      }
    }

    addOrUpdateNode(source,destination)
    addOrUpdateNode(destination, source)
    nodes += source += destination
  }

  def computeAdjacencyReach(n:Int): Int = {
    var setOfAllNodes = Set[Int](n)
    var number = 0
    var currentSet = nodesById.get(n).getOrElse(Set[Int]())
    while (!currentSet.isEmpty) {
      setOfAllNodes ++= currentSet
      number += 1
      currentSet = currentSet.flatMap(n => this.nodesById.get(n).getOrElse(Set[Int]())) -- setOfAllNodes
    }
    number
  }

  def computeMinimalTime(): Int = {
    this.nodes.map(computeAdjacencyReach(_)).min
  }
}
{'fraction_non_alphanumeric': 0.12995205652283623, 'fraction_numerical': 0.004542013626040878, 'mean_word_length': 3.3849557522123894, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4047132', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1283, 'n_words': 347}