diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-02-18 15:11:04 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-02-18 15:11:04 -0800 |
commit | 74ae7d8e8b05babdc8d3972827036fa6f0b2cade (patch) | |
tree | 0e875ee58d6fdf5a568e3f82bf58f590871680ab /day12.fsx | |
parent | f3d54ee9e83324da6c2c8fc90ea2b5d5d749b0a5 (diff) | |
download | AdventOfCode2017-74ae7d8e8b05babdc8d3972827036fa6f0b2cade.tar.gz |
solutions up to day 14
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 |