--- 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?
class Day23
  def initialize()
    @network = Hash.new
  end

  def read_process_input
    File.open(""Day23\\input23".txt", "r") do |f|
      f.each_line.with_index do |line, i|
        computers = line.gsub("\n","").split("-")

        if !@network.key?(computers[0])
          @network[computers[0]] = []
        end 

        if !@network.key?(computers[1])
          @network[computers[1]] = []
        end

        @network[computers[0]].push(computers[1])
        @network[computers[1]].push(computers[0])
      end
    end
  end

  def find_groups
    relevant_grops = Set.new
    visited = []

    for key in @network.keys
      if !visited.include?(key)
        other_computers = @network[key]
        visited.push(key)
        if other_computers.length >= 2
          for i in 0..other_computers.length-1
            other_computer_network = @network[other_computers[i]]
            common_computers = other_computers & other_computer_network
            if common_computers.length > 0
              for j in 0..common_computers.length-1
                last_computer_network = @network[common_computers[j]]
                if last_computer_network.include?(key) && 
                  last_computer_network.include?(other_computers[i])
                  if key.start_with?("t") || common_computers[j].start_with?("t") ||
                    other_computers[i].start_with?("t")
                    relevant_grops.add([key, other_computers[i], common_computers[j]].sort)
                  end
                end
              end
            end 
          end
        end
      end
    end
    return relevant_grops.length
  end
end


day23 = Day23.new()
day23.read_process_input
puts day23.find_groups