#!/usr/bin/env ruby input = (ARGV.first.nil? ? DATA : ARGF) .readlines(chomp: true) .map { _1.split("-") } connections = Hash.new { _1[_2] = Set.new } input.each do |connection| a, b = connection.sort connections[a] << b connections[b] << a end parties = Set.new connections.dup.each do |(node, neighbors)| neighbors.each do |neighbor| connections[neighbor].each do |neighbors_neighbor| if neighbors.include?(neighbors_neighbor) parties << [node, neighbor, neighbors_neighbor].sort end end end end p parties.select { |party| party.any? { _1.match?(/^t/) } }.size largest = [] connections.keys.each do |node| (connections[node].size + 1).downto(1) do |size| (connections[node] + [node]).to_a.combination(size) do |group| if group.all? { |a| group.all? { |b| a == b || connections[a].include?(b) } } if largest.size < group.size largest = group break end end end break if largest.size == size end end p largest.sort.join(",") __END__ 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