d23p2
This commit is contained in:
44
day23/d23p2.swift
Normal file
44
day23/d23p2.swift
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
typealias Network = [String: Set<String>]
|
||||||
|
|
||||||
|
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<String>, p: Set<String>, x: Set<String>
|
||||||
|
) -> Set<String>? {
|
||||||
|
if p.count == 0 && x.count == 0 {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
var maxClique: Set<String>? = 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: ","))
|
||||||
|
|
Reference in New Issue
Block a user