diff --git a/day23/d23p2.swift b/day23/d23p2.swift new file mode 100644 index 0000000..b228fb6 --- /dev/null +++ b/day23/d23p2.swift @@ -0,0 +1,44 @@ +import Foundation + +typealias Network = [String: Set] + +func readInput(_ filePath: String) throws -> Network { + var map: Network = [:] + try String(contentsOfFile: filePath, encoding: .ascii) + .split(separator: "\n").map { $0.split(separator: "-") } + .map { (String($0[0]), String($0[1])) }.forEach { m1, m2 in + map[m1, default: []].insert(m2) + map[m2, default: []].insert(m1) + } + return map +} + +func bronkerbosch( + _ network: Network, r: Set, p: Set, x: Set +) -> Set? { + if p.count == 0 && x.count == 0 { + return r + } + var maxClique: Set? = nil + var nextP = p + var nextX = x + for v in p { + if let clique = bronkerbosch(network, + r: r.union([v]), + p: nextP.intersection(network[v]!), + x: nextX.intersection(network[v]!) + ) { + if clique.count > (maxClique ?? []).count { + maxClique = clique + } + } + nextP.remove(v) + nextX.insert(v) + } + return maxClique +} + +let conns = try readInput(CommandLine.arguments[1]) +let clique = bronkerbosch(conns, r: [], p: Set(conns.keys), x: []) +print(Array(clique ?? []).sorted().joined(separator: ",")) +