Skip to content

Commit

Permalink
day 23 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gereons committed Dec 23, 2024
1 parent 505e53b commit 359b1fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
50 changes: 48 additions & 2 deletions Sources/Day23/Day23.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

import AoCTools
import Collections

final class Day23: AOCDay {
let title = "LAN Party"
Expand Down Expand Up @@ -40,7 +41,52 @@ final class Day23: AOCDay {
return ts.count
}

func part2() -> Int {
0
func part2() -> String {
var cliques = Set<Set<String>>()
for start in Set(connections.map { $0.first }) {
if cliques.contains(where: { $0.contains(start) }) {
continue
}
let clique = findAllConnectedTo(start)
cliques.insert(clique)
}

return cliques
.sorted(by: { $0.count > $1.count })
.first!
.sorted()
.joined(separator: ",")
}

private func findAllConnectedTo(_ start: String) -> Set<String> {
var clique = Set([start])
var candidates = Deque<String>([start])

while let candidate = candidates.popFirst() {
// neighbors of our current candidate
let neighbors = connections.filter { $0.first == candidate }
// only look at ones that are connected to everyone in out clique so far
let nextCandidates = neighbors.filter { isConnected($0.second, clique) }
for next in nextCandidates {
if !clique.contains(next.second) {
clique.insert(next.second)
candidates.append(next.second)
}
}

// remove all entries from clique that aren't connected to everyone else any longer
let copy = clique
for c in copy {
if !isConnected(c, copy) {
clique.remove(c)
}
}
}

return clique
}

private func isConnected(_ node: String, _ clique: Set<String>) -> Bool {
clique.allSatisfy { node == $0 || connections.contains(Pair(node, $0)) }
}
}
4 changes: 2 additions & 2 deletions Tests/Day23Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ struct Day23Tests {
@MainActor @Test("Day 23 Part 2", .tags(.testInput))
func testDay23_part2() {
let day = Day23(input: testInput)
#expect(day.part2() == 0)
#expect(day.part2() == "co,de,ka,ta")
}

@MainActor @Test("Day 23 Part 2 Solution")
func testDay23_part2_solution() {
let day = Day23(input: Day23.input)
#expect(day.part2() == 0)
#expect(day.part2() == "cm,de,ez,gv,hg,iy,or,pw,qu,rs,sn,uc,wq")
}
}

0 comments on commit 359b1fb

Please sign in to comment.