diff options
Diffstat (limited to 'day12.fsx')
-rw-r--r-- | day12.fsx | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/day12.fsx b/day12.fsx new file mode 100644 index 0000000..af458c2 --- /dev/null +++ b/day12.fsx @@ -0,0 +1,36 @@ +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<string, list<string>>) (visited: HashSet<string>) origin = + if not (visited.Add(origin)) then () + else + for child in connections[origin] do visit connections visited child + +let checkInGroups (groups: seq<HashSet<string>>) (origin:string) = + Seq.fold (fun acc (group: HashSet<string>) -> acc || group.Contains(origin)) false groups + +let () = + let pipes = new Dictionary<string, list<string>>() + File.ReadAllLines "day12.txt" |> Array.map getConnection |> Array.iter (fun (head, tail) -> pipes[head] <- tail) + + let reachable = new HashSet<string>() + visit pipes reachable "0" + printfn "%A" reachable.Count + + let groups = new List<HashSet<string>>() + for origin in pipes.Keys do + if checkInGroups groups origin then () + else + let newGroup = new HashSet<string>() + visit pipes newGroup origin + groups.Add(newGroup) + + printfn "%A" groups.Count |