blob: af458c2a3e6bf9cb5051ec16c08887e19aaecde7 (
plain) (
tree)
|
|
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
|