repl.it
@jajoosam/

r-g-hist-chain

Nodejs

No description

fork
loading
Files
  • index.js
  • config.json
  • history.json
index.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
39
40
41
42
43
44
45
const history = require("./history.json");

// taken from 

const findChain = (index, chain = history[index].buyer) => {
    const {buyer, receiver} = history[index];
    let longestChain = chain, payedForward = false;

    for(let i = index+1; i < history.length; ++i) {
        if(history[i].buyer == receiver) {
            payedForward = true;
            let c = findChain(i, chain + " -> " + receiver);
            if(c.split(" -> ").length > longestChain.split(" -> ").length) {
                longestChain = c;
            }
        }
    }
    return longestChain + (payedForward ? "" : ` -> ${receiver}`);
}

let chainsArray = history.map((e,i)=> {
  return {
    name: e.buyer,
    chain: findChain(i)
  }
})

let maxChains = {};

chainsArray.forEach(e=>{
  if(!maxChains[e.name]){
    return maxChains[e.name] = e.chain;
  }
  if(maxChains[e.name].split(" -> ").length < e.chain.split(" -> ").length){
    return maxChains[e.name] = e.chain;
  }
})


let maxLength = Math.max(...chainsArray.map(e=> e.chain.split(' -> ').length))

let longestChains = chainsArray.filter(e=> e.chain.split(' -> ').length === maxLength);


console.log(longestChains)
?