<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>
<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">
<None Include="Data\day2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+ <None Include="Data\day4.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
--- /dev/null
+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
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
--- /dev/null
+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
<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>