From: Ummon Date: Mon, 4 Dec 2017 06:59:19 +0000 (+0100) Subject: Day 4 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=ebf413fa7d8fe43e919895fcad719bcf679f3ab2;p=advent_of_code_2017.git Day 4 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 01ee4ac..c2c8a6c 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 3 + 4 pdbonly @@ -59,6 +59,7 @@ + @@ -67,6 +68,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day4.fs b/AdventOfCode2017/Day4.fs new file mode 100644 index 0000000..995715f --- /dev/null +++ b/AdventOfCode2017/Day4.fs @@ -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 diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 2c103a7..f0c7ca1 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -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 index 0000000..3057786 --- /dev/null +++ b/Tests/Day4 tests.fs @@ -0,0 +1,23 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day4 tests`` (output : ITestOutputHelper) = + + [] + 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") + + [] + 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 diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index a0ec95c..9721c8e 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -58,6 +58,7 @@ +