Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.27 KB | Source Code | 0 0
  1. import Foundation
  2.  
  3. struct Graph {
  4.     private(set) var nodes = [String: Set<String>]()
  5.    
  6.     mutating func addConnection(_ node1: String, _ node2: String) {
  7.         nodes[node1, default: Set<String>()].insert(node2)
  8.         nodes[node2, default: Set<String>()].insert(node1)
  9.     }
  10.    
  11.     // Return true if all elements of the input set are interconnected by repeatedly removing elements _not_ common to
  12.     // each and seeing if any were removed.
  13.     func isInterconnected(_ input: Set<String>) -> Bool {
  14.         var interconnected = input
  15.        
  16.         for item in input {
  17.             var reciprocalConnections = self.nodes[item]!
  18.             reciprocalConnections.insert(item)
  19.            
  20.             interconnected = interconnected.intersection(reciprocalConnections)
  21.            
  22.             if interconnected.count < input.count {
  23.                 return false
  24.             }
  25.         }
  26.        
  27.         return true
  28.     }
  29. }
  30.  
  31. extension Set where Element == String {
  32.     func combinations(of size: Int) -> [Set<String>] {
  33.         guard self.count >= size else {
  34.             return []
  35.         }
  36.        
  37.         let elements = Array(self)
  38.         var combos = [Set<String>]()
  39.        
  40.         func combine(_ start: Int, _ current: [String]) {
  41.             if current.count == size {
  42.                 combos.append(Set(current))
  43.                 return
  44.             }
  45.            
  46.             for i in start..<elements.count {
  47.                 var nextCombination = current
  48.                 nextCombination.append(elements[i])
  49.                
  50.                 combine(i + 1, nextCombination)
  51.             }
  52.         }
  53.        
  54.         combine(0, [])
  55.         return combos
  56.     }
  57. }
  58.  
  59. let graph = input(forDay: 23)
  60.     .split(separator: "\n")
  61.     .map { $0.split(separator: "-") }
  62.     .reduce(into: Graph()) { graph, nodes in
  63.         graph.addConnection(String(nodes[0]), String(nodes[1]))
  64.     }
  65.  
  66. func partOne() -> Int {
  67.     var lans = Set<Set<String>>()
  68.    
  69. outer:
  70.     for (from, to) in graph.nodes {
  71.         var lan = to
  72.         lan.insert(from)
  73.        
  74.         if lan.count < 3 {
  75.             continue
  76.         }
  77.        
  78.         for combo in lan.combinations(of: 3) {
  79.             if graph.isInterconnected(combo) && combo.contains(where: { $0.first == "t" }) {
  80.                 lans.insert(combo)
  81.             }
  82.         }
  83.     }
  84.    
  85.     return lans.count
  86. }
  87.  
  88. func partTwo() -> String {
  89.     var largestLan = Set<String>()
  90.    
  91. outer:
  92.     for (from, to) in graph.nodes {
  93.         var lan = to
  94.         lan.insert(from)
  95.        
  96.         for i in (1..<lan.count).reversed() {
  97.             // No point checking combinations shorter than the largest one we've seen
  98.             if i <= largestLan.count {
  99.                 continue outer
  100.             }
  101.            
  102.             for combo in lan.combinations(of: i) {
  103.                 // We're starting largest combo first, so as soon as we find one we know it's the largest across all
  104.                 // nodes in this network
  105.                 if graph.isInterconnected(combo) {
  106.                     largestLan = combo
  107.                     continue outer
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     return Array(largestLan).sorted().joined(separator: ",")
  114. }
  115.  
  116. print(partOne())
  117. print(partTwo())
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement