Day 12
authorGreg Burri <greg.burri@gmail.com>
Tue, 12 Dec 2017 05:30:28 +0000 (06:30 +0100)
committerGreg Burri <greg.burri@gmail.com>
Tue, 12 Dec 2017 05:30:28 +0000 (06:30 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day12.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day12 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index 3596929..da66644 100644 (file)
@@ -25,7 +25,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>11</StartArguments>
+    <StartArguments>12</StartArguments>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
     <Compile Include="Day9.fs" />
     <Compile Include="Day10.fs" />
     <Compile Include="Day11.fs" />
+    <Compile Include="Day12.fs" />
     <Compile Include="Program.fs" />
     <None Include="App.config" />
-    <None Include="Data\day1.input">
+    <Content Include="Data\day1.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day2.input">
+    </Content>
+    <Content Include="Data\day2.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day4.input">
+    </Content>
+    <Content Include="Data\day4.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day5.input">
+    </Content>
+    <Content Include="Data\day5.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day6.input">
+    </Content>
+    <Content Include="Data\day6.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day7.input">
+    </Content>
+    <Content Include="Data\day7.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day8.input">
+    </Content>
+    <Content Include="Data\day8.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day9.input">
+    </Content>
+    <Content Include="Data\day9.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-    <None Include="Data\day11.input">
+    </Content>
+    <Content Include="Data\day11.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
+    </Content>
+    <Content Include="Data\day12.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day12.fs b/AdventOfCode2017/Day12.fs
new file mode 100644 (file)
index 0000000..0a61c9b
--- /dev/null
@@ -0,0 +1,46 @@
+module AdventOfCode2017.Day12
+
+open System
+open System.Linq
+open System.Collections.Generic
+
+type Graph =
+    {
+        Name : string
+        Neighbors : List<Graph>
+    }
+
+let parseInput (lines : string[]) : (Graph * string[]) list =
+    [
+        for line in lines do
+            let a = line.Split ([| ' '; ',' |], StringSplitOptions.RemoveEmptyEntries)
+            yield { Name = a.[0]; Neighbors = List<Graph> () }, a.[2 .. a.Length - 1]
+    ]
+
+let f (input : (Graph * string[]) list) =
+    let toVisit = Dictionary<string, Graph> ()
+
+    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<Dictionary<string, Graph>> ()
+
+    let rec visit (g : Graph) (dic : Dictionary<string, Graph>) =
+        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<string, Graph> ()
+        visited.Add dic
+        visit (toVisit.First().Value) dic
+
+    visited.First().Count, visited.Count
+
index fe5954f..9b1269a 100644 (file)
@@ -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 (file)
index 0000000..96d3438
--- /dev/null
@@ -0,0 +1,17 @@
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day12 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        ()
+
+    [<Fact>]
+    let ``(Part2) From web page`` () =
+        ()
\ No newline at end of file
index 21e504c..a6097ba 100644 (file)
@@ -66,6 +66,7 @@
     <Compile Include="Day9 tests.fs" />
     <Compile Include="Day10 tests.fs" />
     <Compile Include="Day11 tests.fs" />
+    <Compile Include="Day12 tests.fs" />
     <Content Include="packages.config" />
     <Content Include="App.config" />
   </ItemGroup>