From ce0db9bcf1e6668892e3c2002432ff7af3b1ea85 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Tue, 12 Dec 2017 06:30:28 +0100 Subject: [PATCH] Day 12 --- AdventOfCode2017/AdventOfCode2017.fsproj | 42 ++++++++++++---------- AdventOfCode2017/Day12.fs | 46 ++++++++++++++++++++++++ AdventOfCode2017/Program.fs | 6 ++++ Tests/Day12 tests.fs | 17 +++++++++ Tests/Tests.fsproj | 1 + 5 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 AdventOfCode2017/Day12.fs create mode 100644 Tests/Day12 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 3596929..da66644 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 11 + 12 pdbonly @@ -67,35 +67,39 @@ + - + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - - + + PreserveNewest - + + + PreserveNewest + diff --git a/AdventOfCode2017/Day12.fs b/AdventOfCode2017/Day12.fs new file mode 100644 index 0000000..0a61c9b --- /dev/null +++ b/AdventOfCode2017/Day12.fs @@ -0,0 +1,46 @@ +module AdventOfCode2017.Day12 + +open System +open System.Linq +open System.Collections.Generic + +type Graph = + { + Name : string + Neighbors : List + } + +let parseInput (lines : string[]) : (Graph * string[]) list = + [ + for line in lines do + let a = line.Split ([| ' '; ',' |], StringSplitOptions.RemoveEmptyEntries) + yield { Name = a.[0]; Neighbors = List () }, a.[2 .. a.Length - 1] + ] + +let f (input : (Graph * string[]) list) = + let toVisit = Dictionary () + + for g, names 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') + + let visited = List> () + + let rec visit (g : Graph) (dic : Dictionary) = + if dic.ContainsKey g.Name |> not then + toVisit.Remove g.Name |> ignore + dic.Add (g.Name, g) + for n in g.Neighbors do + visit n dic + + while toVisit.Count > 0 do + let dic = Dictionary () + visited.Add dic + visit (toVisit.First().Value) dic + + visited.First().Count, visited.Count + diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index fe5954f..9b1269a 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -52,6 +52,11 @@ let day11 () = let part1, part2 = Day11.distanceInHex input sprintf "part1 = %A, part2 = %A" part1 part2 +let day12 () = + let input = File.ReadAllLines "Data/day12.input" |> Day12.parseInput + let part1, part2 = Day12.f input + sprintf "part1 = %A, part2 = %A" part1 part2 + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -68,6 +73,7 @@ let doDay (n : int) = | 9 -> day9 () | 10 -> day10 () | 11 -> day11 () + | 12 -> day12 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day12 tests.fs b/Tests/Day12 tests.fs new file mode 100644 index 0000000..96d3438 --- /dev/null +++ b/Tests/Day12 tests.fs @@ -0,0 +1,17 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day12 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + () + + [] + let ``(Part2) From web page`` () = + () \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 21e504c..a6097ba 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -66,6 +66,7 @@ + -- 2.45.2