From: Ummon Date: Tue, 12 Dec 2017 13:12:18 +0000 (+0100) Subject: More concise X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=103478452c6f43d10083c21b8de022e7d43e2fc3;p=advent_of_code_2017.git More concise --- diff --git a/AdventOfCode2017/Day12.fs b/AdventOfCode2017/Day12.fs index 10dae1f..fded72f 100644 --- a/AdventOfCode2017/Day12.fs +++ b/AdventOfCode2017/Day12.fs @@ -11,13 +11,14 @@ let parseInput (lines : string[]) : Map> = ) |> Map.ofArray let graphCount (g : Map>) = - let rec visit (current : int) (visited : Set) : Set = + let rec visit (visited : Set) (current : int) : Set = if visited |> Set.contains current then Set.empty else - seq { yield g.[current]; for neighbor in g.[current] -> visit neighbor (visited |> Set.add current) } |> Set.unionMany + let visited' = visited.Add current + g.[current] + (g.[current] |> Set.map (visit visited') |> Set.unionMany) let rec nbRoots (vertices : Set) = - if Set.isEmpty vertices then 0 else 1 + nbRoots (vertices - (visit (vertices |> Set.minElement) Set.empty)) + if Set.isEmpty vertices then 0 else 1 + nbRoots (vertices - (visit Set.empty (vertices |> Set.minElement))) - visit 0 Set.empty |> Set.count, g |> Map.toList |> List.map fst |> Set.ofList |> nbRoots \ No newline at end of file + visit Set.empty 0 |> Set.count, g |> Map.toList |> List.map fst |> Set.ofList |> nbRoots \ No newline at end of file