Get infected parameters starting from a specific carrier

Imagine one guy got infected by a virus and you want to detect potential infections by direct contact. We have an array that represents the city where he lives and subarrays that represent the houses where he went. We are gonna use numbers (people identification) for our example.

Updated version using Set properly thanks to japanfever

8 replies
  1. japanfever
    japanfever says:

    //blog code tag not working so you probably cant see my solution
    //japanfever returns E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
    function get_infected(structure, carrier) {
    var tree = {}
    structure.forEach(([a, b]) => {
    tree[a] = (new Set(tree[a])).add(b)
    tree[b] = (new Set(tree[b])).add(a)
    })
    return carrier.reduce((acc, sub) => acc.concat(Array.from(tree[sub])), carrier)
    }

    • Génesis
      Génesis says:

      Dude, that’s nice xD well done, but it doesn’t work when you change the quantity of people living in the houses. For example, try: structure = [[0, 1, 5], [0, 4], [2, 3], [1, 4], [5, 6]]

  2. japanfever
    japanfever says:
    //japanfever the best E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
    function get_infected(structure, carrier) {
      carrier = new Set(carrier)
      let n = 0
      while (n != carrier.size) {
        let rest = [...carrier.values()].slice(n)
        n = carrier.size
        structure.forEach(arr => arr.findIndex(x => rest.includes(x)) >= 0 &&
                                 arr.forEach(v => carrier.add(v)))
      }
      return Array.from(carrier)
    }
    

Comments are closed.