From ce55f2b39f58e803f04a782a7fe799ce58234f1d Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Tue, 12 Dec 2017 06:42:49 +0100 Subject: [PATCH] More efficient --- AdventOfCode2017/Day12.fs | 13 +++++++------ AdventOfCode2017/Program.fs | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/AdventOfCode2017/Day12.fs b/AdventOfCode2017/Day12.fs index 8d840f1..1f83f1d 100644 --- a/AdventOfCode2017/Day12.fs +++ b/AdventOfCode2017/Day12.fs @@ -20,13 +20,14 @@ let parseInput (lines : string[]) : (Graph * string[]) list = let graphCount (input : (Graph * string[]) list) = let toVisit = Dictionary () - for g, names in input do + for g, _ in input do toVisit.Add (g.Name, g) - for name in names do - for g', _ in input do - if g'.Name = name then - g'.Neighbors.Add (g) - g.Neighbors.Add (g') + + for g, neighbors in input do + for neighbor in neighbors do + let g' = toVisit.[neighbor] + g'.Neighbors.Add g + g.Neighbors.Add g' let visitedGroups = List> () diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 9b1269a..933a60a 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -54,7 +54,7 @@ let day11 () = let day12 () = let input = File.ReadAllLines "Data/day12.input" |> Day12.parseInput - let part1, part2 = Day12.f input + let part1, part2 = Day12.graphCount input sprintf "part1 = %A, part2 = %A" part1 part2 let doDay (n : int) = -- 2.45.2