Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import _ from 'lodash'
  2. import loadData from '../../helpers/loadData.mjs'
  3.  
  4. const inputData = loadData('./data.txt').map((line) => line.split('-'))
  5.  
  6. // console.log(inputData)
  7.  
  8. const net = new Map()
  9.  
  10. for(const [a,b] of inputData) {
  11.   if(!net.has(a)) net.set(a, [])
  12.   if(!net.has(b)) net.set(b, [])
  13.  
  14.   net.get(a).push(b)
  15.   net.get(b).push(a)
  16. }
  17.  
  18. // console.log(net)
  19.  
  20. let found = new Set()
  21.  
  22. for(let [key1, conn1] of net.entries()) {
  23.   const cc = [key1, ...conn1]
  24.   const other = conn1.map((key2) => [key2, ...net.get(key2)])
  25.  
  26.   const w = other.map((s) => _.intersection(cc, s).length)
  27.   const maxw = _.max(w)
  28.   if(w.filter(v => v === maxw).length < maxw - 1) continue
  29.   if(maxw < found.size) continue
  30.  
  31.   let base = [...cc]
  32.   for(let i = 0; i < other.length && base.length >= maxw; i++) {
  33.     if(w[i] !== maxw) continue
  34.  
  35.     base = _.intersection(base, other[i])
  36.   }
  37.   if(base.length < maxw) continue
  38.   const res = new Set(base.sort())
  39.   if(_.isEqual(found, res)) continue
  40.   found = res
  41. }
  42.  
  43. console.log(Array.from(found).join(','))
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement