function inferConnections(signals) {
let possibleAssociations = new Map([
['a', new Set(['e','g'])],
['b', new Set(['e','g'])],
['c', new Set(['e','g'])],
['d', new Set(['e','g'])],
['e', new Set(['e','g'])],
['f', new Set(['e','g'])],
['g', new Set(['e','g'])]
])
let one = signals.filter( signal => signal.size == 2)[0]
possibleAssociations.forEach( (associations, wire) => {
if (one.has(wire))
possibleAssociations.set(wire, new Set(["c", "f"]))
})
let seven = signals.filter( signal => signal.size == 3)[0]
possibleAssociations.forEach( (associations, wire) => {
if (seven.has(wire) && !associations.has("c"))
possibleAssociations.set(wire, new Set(["a"]))
})
let four = signals.filter( signal => signal.size == 4)[0]
possibleAssociations.forEach( (associations, wire) => {
if (four.has(wire) && !associations.has("c"))
possibleAssociations.set(wire, new Set(["b", "d"]))
})
let sizeFive = signals.filter( signal => signal.size == 5)
let allWires = new Set(["a","b","c","d","e","f","g"])
sizeFive.map( signal => Array.from(setDifference(allWires, signal)))
.flat()
.reduce( (acc, curr) => {
if (acc.has(curr))
acc.set(curr, acc.get(curr) + 1)
else
acc.set(curr, 1)
return acc
}, new Map())
.forEach( (count, wire) => {
if (count === 2) {
let prevAssoc = possibleAssociations.get(wire)
prevAssoc.delete("d")
prevAssoc.delete("g")
possibleAssociations.set(wire, prevAssoc)
}
})
possibleAssociations.forEach( (associations, wire) => {
if (associations.has("d"))
possibleAssociations.set(wire, new Set(["d"]))
if (associations.has("g"))
possibleAssociations.set(wire, new Set(["g"]))
})
let sizeSix = signals.filter( signal => signal.size == 6)
let wireToC = sizeSix.map( signal => Array.from(setDifference(allWires, signal)))
.flat()
.filter( wire => possibleAssociations.get(wire).size === 2)[0]
possibleAssociations.set(wireToC, new Set(["c"]))
possibleAssociations.forEach( (associations, wire) => {
if (associations.has("f"))
possibleAssociations.set(wire, new Set(["f"]))
})
possibleAssociations.forEach( (association, wire) => {
possibleAssociations.set(wire, Array.from(association)[0])
})
return possibleAssociations
}