--- Day 23: LAN Party ---

As The Historians wander around a secure area at Easter Bunny HQ, you come across posters for a LAN party scheduled for today! Maybe you can find it; you connect to a nearby datalink port and download a map of the local network (your puzzle input).

The network map provides a list of every connection between two computers. For example:

kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn
Each line of text in the network map represents a single connection; the line kh-tc represents a connection between the computer named kh and the computer named tc. Connections aren't directional; tc-kh would mean exactly the same thing.

LAN parties typically involve multiplayer games, so maybe you can locate it by finding groups of connected computers. Start by looking for sets of three computers where each computer in the set is connected to the other two computers.

In this example, there are 12 such sets of three inter-connected computers:

aq,cg,yn
aq,vc,wq
co,de,ka
co,de,ta
co,ka,ta
de,ka,ta
kh,qp,ub
qp,td,wh
tb,vc,wq
tc,td,wh
td,wh,yn
ub,vc,wq
If the Chief Historian is here, and he's at the LAN party, it would be best to know that right away. You're pretty sure his computer's name starts with t, so consider only sets of three computers where at least one computer's name starts with t. That narrows the list down to 7 sets of three inter-connected computers:

co,de,ta
co,ka,ta
de,ka,ta
qp,td,wh
tb,vc,wq
tc,td,wh
td,wh,yn
Find all the sets of three inter-connected computers. How many contain at least one computer with a name that starts with t?

Your puzzle answer was 1194.

--- Part Two ---

There are still way too many results to go through them all. You'll have to find the LAN party another way and go there yourself.

Since it doesn't seem like any employees are around, you figure they must all be at the LAN party. If that's true, the LAN party will be the largest set of computers that are all connected to each other. That is, for each computer at the LAN party, that computer will have a connection to every other computer at the LAN party.

In the above example, the largest set of computers that are all connected to each other is made up of co, de, ka, and ta. Each computer in this set has a connection to every other computer in the set:

ka-co
ta-co
de-co
ta-ka
de-ta
ka-de
The LAN party posters say that the password to get into the LAN party is the name of every computer at the LAN party, sorted alphabetically, then joined together with commas. (The people running the LAN party are clearly a bunch of nerds.) In this example, the password would be co,de,ka,ta.

What is the password to get into the LAN party?
// solution for https://adventofcode.com/2024/day/23 part 2

"use strict"

const input = Deno.readTextFileSync("day23-input.txt").trim()

const allConnections = { }

const allNetworks = [ ]

var greatestNetworkLength = 0

var bestNetwork = [ ]


function main() {

    processInput() 
    
    fillAllNetworks()
    
    search()

    console.log("the answer is", bestNetwork.sort().join(","))
}

///////////////////////////////////////////////////////////////////////////////

function processInput() {
    
    const rawLines = input.split("\n")
    
    for (const rawLine of rawLines) { 
    
        const pair = rawLine.trim().split("-")
        
        const a = pair.shift()
        const b = pair.shift()
        
        if (allConnections[a] == undefined) { allConnections[a] = [ ] }
        if (allConnections[b] == undefined) { allConnections[b] = [ ] }
        
        allConnections[a].push(b)
        allConnections[b].push(a)
    }
}

function fillAllNetworks() {

    const allComputers = Object.keys(allConnections)
    
    for (const computer of allComputers) {
    
        const network = allConnections[computer].slice()
        
        network.unshift(computer)
        
        allNetworks.push(network)
        
        if (network.length > greatestNetworkLength) { greatestNetworkLength = network.length } 
    }
}

///////////////////////////////////////////////////////////////////////////////

function search() {
    
    for (const network of allNetworks) { // searches without removing any member
    
        if (! allMembersAreFriends(network)) { continue }
    
        if (network.length > bestNetwork.length) { bestNetwork = network }
        
        if (bestNetwork.length == greatestNetworkLength) { return }
    }

    while (true) {
    
        searchRemovingTheWorstMember()
        
        if (bestNetwork.length != 0) { return }
    }
}

function searchRemovingTheWorstMember() {
    
    greatestNetworkLength -= 1
    
    for (const network of allNetworks) {
    
        removeTheWorstMember(network)
    
        if (! allMembersAreFriends(network)) { continue }
    
        if (network.length > bestNetwork.length) { bestNetwork = network }
        
        if (bestNetwork.length == greatestNetworkLength) { return }
    }
}

///////////////////////////////////////////////////////////////////////////////

function removeTheWorstMember(network) { // expects network having at least one bad member

    const unfriendship = [ ]
    
    for (let n = 0; n < network.length; n++) { unfriendship.push(0) }
    
    
    let worstIndex = 0
    let worstValue = -1

    const off = network.length
    
    for (let a = 0; a < off - 1; a++) {
    
        const computerA = network[a]
    
        const connectionsA = allConnections[computerA]
        
        for (let b = a + 1; b < off; b++) {
        
            const computerB = network[b] 
            
            if (connectionsA.includes(computerB)) { continue }
            
            unfriendship[a] += 1
            unfriendship[b] += 1
            
            if (unfriendship[a] > worstValue) { worstValue = unfriendship[a]; worstIndex = a }
            if (unfriendship[b] > worstValue) { worstValue = unfriendship[b]; worstIndex = b }
        }
    }
    
 //  if (worstIndex == -1) { return } // not needed
    
    network.splice(worstIndex, 1)
}

///////////////////////////////////////////////////////////////////////////////

function allMembersAreFriends(network) {

    const off = network.length
    
    for (let a = 0; a < off - 1; a++) {
    
        const computerA = network[a]
    
        const connectionsA = allConnections[computerA]
        
        for (let b = a + 1; b < off; b++) {
        
            const computerB = network[b] 
            
            if (! connectionsA.includes(computerB)) { return false }
        }
    }    
    
    return true
}

///////////////////////////////////////////////////////////////////////////////

console.time("execution time")
main()
console.timeEnd("execution time") // 5ms