From 103478452c6f43d10083c21b8de022e7d43e2fc3 Mon Sep 17 00:00:00 2001 From: Ummon Date: Tue, 12 Dec 2017 14:12:18 +0100 Subject: [PATCH] More concise --- AdventOfCode2017/Day12.fs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 -- 2.45.2