open System.IO open System.Text.RegularExpressions open System.Collections.Generic let getConnection line = let idRegex = new Regex("\d+") let ids = idRegex.Matches(line) match [for ID in ids -> ID.Value] with | [] -> ("", []) | head::tail -> (head, tail) let rec visit (connections: Dictionary>) (visited: HashSet) origin = if not (visited.Add(origin)) then () else for child in connections[origin] do visit connections visited child let checkInGroups (groups: seq>) (origin:string) = Seq.fold (fun acc (group: HashSet) -> acc || group.Contains(origin)) false groups let () = let pipes = new Dictionary>() File.ReadAllLines "day12.txt" |> Array.map getConnection |> Array.iter (fun (head, tail) -> pipes[head] <- tail) let reachable = new HashSet() visit pipes reachable "0" printfn "%A" reachable.Count let groups = new List>() for origin in pipes.Keys do if checkInGroups groups origin then () else let newGroup = new HashSet() visit pipes newGroup origin groups.Add(newGroup) printfn "%A" groups.Count