-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge17.js
38 lines (32 loc) · 1.12 KB
/
challenge17.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//https://adventjs.dev/challenges/17
export default function countPackages(carriers, carrierID) {
// ¡No olvides compartir tu solución en redes!
const getTransportista = (sub, idCarrier) => {
let transportista = sub.find((item) => item[0] === idCarrier)
let subordinados = transportista[2]
let nroPaquetes = transportista[1]
subordinados.forEach((item) => {
nroPaquetes += getTransportista(carriers, item)
})
return nroPaquetes
}
return getTransportista(carriers, carrierID)
}
const carriers = [
['dapelu', 5, ['midu', 'jelowing']],
['midu', 2, []],
['jelowing', 2, []],
]
console.log(countPackages(carriers, 'dapelu')) // 9
// 5 de dapelu, 2 de midu y 2 de jelowing = 9
const carriers2 = [
['lolivier', 8, ['camila', 'jesuspoleo']],
['camila', 5, ['sergiomartinez', 'conchaasensio']],
['jesuspoleo', 4, []],
['sergiomartinez', 4, []],
['conchaasensio', 3, ['facundocapua', 'faviola']],
['facundocapua', 2, []],
['faviola', 1, []],
]
console.log(countPackages(carriers2, 'camila')) // 15
// 5 de camila, 4 de sergiomartinez, 3 de conchaasensio, 2 de facundocapua y 1 de faviola = 15