Day 4
authorUmmon <greg.burri@gmail.com>
Mon, 4 Dec 2017 06:59:19 +0000 (07:59 +0100)
committerUmmon <greg.burri@gmail.com>
Mon, 4 Dec 2017 06:59:19 +0000 (07:59 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day4.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day4 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index 01ee4ac..c2c8a6c 100644 (file)
@@ -25,7 +25,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>3</StartArguments>
+    <StartArguments>4</StartArguments>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -59,6 +59,7 @@
     <Compile Include="Day1.fs" />
     <Compile Include="Day2.fs" />
     <Compile Include="Day3.fs" />
+    <Compile Include="Day4.fs" />
     <Compile Include="Program.fs" />
     <None Include="App.config" />
     <None Include="Data\day1">
@@ -67,6 +68,9 @@
     <None Include="Data\day2">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Include="Data\day4.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day4.fs b/AdventOfCode2017/Day4.fs
new file mode 100644 (file)
index 0000000..995715f
--- /dev/null
@@ -0,0 +1,20 @@
+module AdventOfCode2017.Day4
+
+open System
+
+let forallDistinctPairs (f : string -> string -> bool) (pp : string) =
+    let words = pp.Split ' '
+    [
+        for a = 0 to words.Length - 1 do
+            for b in a + 1 .. words.Length - 1 -> f words.[a] words.[b]
+    ] |> List.forall not
+
+let passphraseValid = forallDistinctPairs (=)
+
+let isAnagram (w1 : string) (w2 : string) =
+    (w1.ToCharArray () |> Array.sort) = (w2.ToCharArray () |> Array.sort)
+
+let passphraseValidAnagram = forallDistinctPairs isAnagram
+
+let nbPassphrasesValid (f : string -> bool) (pps : string seq) =
+    pps |> Seq.map f |> Seq.fold (fun sum valid -> if valid then sum + 1 else sum) 0
\ No newline at end of file
index 2c103a7..f0c7ca1 100644 (file)
@@ -14,11 +14,16 @@ let day3 () =
     let input = 325489
     sprintf "part1 = %A, part2 = %A" (Day3.spiralManhattanDistanceSum input) (Day3.spiralAdjacentSumBiggerThan input)
 
+let day4 () =
+    let input = File.ReadAllLines "Data/day4.input"
+    sprintf "part1 = %A, part2 = %A" (Day4.nbPassphrasesValid Day4.passphraseValid input) (Day4.nbPassphrasesValid Day4.passphraseValidAnagram input)
+
 let doDay (n : int) =
     let result =
         match n with
         | 2 -> day2 ()
         | 3 -> day3 ()
+        | 4 -> day4 ()
         | _ -> day1 ()
     printfn "Result of day %i: %s" n result
 
diff --git a/Tests/Day4 tests.fs b/Tests/Day4 tests.fs
new file mode 100644 (file)
index 0000000..3057786
--- /dev/null
@@ -0,0 +1,23 @@
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day4 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        Assert.True (Day4.passphraseValid "aa bb cc dd ee")
+        Assert.False (Day4.passphraseValid "aa bb cc dd aa")
+        Assert.True (Day4.passphraseValid "aa bb cc dd aaa")
+
+    [<Fact>]
+    let ``(Part2) From web page`` () =
+        Assert.True (Day4.passphraseValidAnagram "abcde fghij")
+        Assert.False (Day4.passphraseValidAnagram "abcde xyz ecdab")
+        Assert.True (Day4.passphraseValidAnagram "a ab abc abd abf abj")
+        Assert.True (Day4.passphraseValidAnagram "iiii oiii ooii oooi oooo")
+        Assert.False (Day4.passphraseValidAnagram "oiii ioii iioi iiio")
\ No newline at end of file
index a0ec95c..9721c8e 100644 (file)
@@ -58,6 +58,7 @@
     <Compile Include="Day1 tests.fs" />
     <Compile Include="Day2 tests.fs" />
     <Compile Include="Day3 tests.fs" />
+    <Compile Include="Day4 tests.fs" />
     <Content Include="packages.config" />
     <Content Include="App.config" />
   </ItemGroup>