From: Greg Burri Date: Mon, 3 Dec 2018 19:53:52 +0000 (+0100) Subject: Day 2 + day 1 unit tests. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=6221223c7fbf2e11f5f17bf0c388f11286df92e2;p=advent_of_code_2018.git Day 2 + day 1 unit tests. --- diff --git a/Day01.fs b/Day01.fs index 449fde1..2c75701 100644 --- a/Day01.fs +++ b/Day01.fs @@ -3,7 +3,7 @@ module AdventOfCode2018.Day01 open System let parseInput (str : string) : int[] = - str.Split ([| "\r\n"; "\r"; "\n" |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + str.Split ([| "\r\n"; "\r"; "\n"; "," |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int let finalFrequency : seq -> int = Seq.sum @@ -18,5 +18,5 @@ let firstDuplicate (changes : seq) : int = else (false, sum', Set.add sum' frequencies) - ) (false, 0, Set.empty) repeatedChanges + ) (false, 0, Set.ofList [ 0 ]) repeatedChanges |> Seq.pick (fun (duplicate, sum, _) -> if duplicate then Some sum else None) diff --git a/Day02.fs b/Day02.fs index af2ebf4..b0c55c4 100644 --- a/Day02.fs +++ b/Day02.fs @@ -3,8 +3,26 @@ module AdventOfCode2018.Day02 open System let parseInput (str : string) : string[] = - str.Split ([| "\r\n"; "\r"; "\n" |], StringSplitOptions.RemoveEmptyEntries) + str.Split ([| "\r\n"; "\r"; "\n"; ","; " " |], StringSplitOptions.RemoveEmptyEntries) -let containsN (id : string) : bool = - false +let containsN (n : int) (id : string) : bool = + id |> Seq.map (fun c -> id |> Seq.filter ((=) c) |> Seq.length) |> Seq.contains n +let checksum (ids : string[]) : int = + let nbIds (n : int) = ids |> Seq.filter (containsN n) |> Seq.length + nbIds 2 * nbIds 3 + +let commonChars (a : string) (b : string) : string = + Seq.zip a b |> Seq.choose (fun (c1, c2) -> if c1 = c2 then Some c1 else None) |> String.Concat + +let findTwoStingsWithCommonChars (l : string array) (n : int) : string = + let rec find i = + match + (l.[i..] |> Array.tryPick ( + fun str -> + let common = commonChars l.[i] str + if common.Length = n then Some common else None + )) with + | Some found -> found + | None -> find (i + 1) + find 0 diff --git a/Program.fs b/Program.fs index b1bd55c..4905062 100644 --- a/Program.fs +++ b/Program.fs @@ -8,8 +8,9 @@ let day01 () = sprintf "part1 = %A, part2 = %A" (Day01.finalFrequency changes) (Day01.firstDuplicate changes) let day02 () = - //let input = File.ReadAllText "Data/day02.input" |> Day01.parseInput - "day02" + let ids = File.ReadAllText "Data/day02.input" |> Day02.parseInput + let idLength = ids.[0].Length + sprintf "part1 = %A, part2 = %A" (Day02.checksum ids) (Day02.findTwoStingsWithCommonChars ids (idLength - 1)) let days : (unit -> string) array = [| diff --git a/Tests/Day01 tests.fs b/Tests/Day01 tests.fs index 6e1290d..b9d2395 100644 --- a/Tests/Day01 tests.fs +++ b/Tests/Day01 tests.fs @@ -1,4 +1,4 @@ -module AdventOfCode2018.Tests +namespace AdventOfCode2018.Tests open System @@ -11,5 +11,15 @@ open AdventOfCode2018 type ``Day01 tests`` (output : ITestOutputHelper) = [] - let ``(Part1) My test`` () = - 1 =! 2 + let ``(Part1) From web page`` () = + Day01.parseInput "+1, +1, +1" |> Day01.finalFrequency =! 3 + Day01.parseInput "+1, +1, -2" |> Day01.finalFrequency =! 0 + Day01.parseInput "-1, -2, -3" |> Day01.finalFrequency =! -6 + + [] + let ``(Part2) From web page`` () = + Day01.parseInput "+1, -1" |> Day01.firstDuplicate =! 0 + Day01.parseInput "+3, +3, +4, -2, -4" |> Day01.firstDuplicate =! 10 + Day01.parseInput "-6, +3, +8, +5, -6" |> Day01.firstDuplicate =! 5 + Day01.parseInput "+7, +7, -2, -7, -4" |> Day01.firstDuplicate =! 14 + diff --git a/Tests/Day02 tests.fs b/Tests/Day02 tests.fs new file mode 100644 index 0000000..e7ab418 --- /dev/null +++ b/Tests/Day02 tests.fs @@ -0,0 +1,44 @@ +namespace AdventOfCode2018.Tests + +open System + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2018 + +type ``Day02 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day02.containsN 2 "abcdef" =! false + Day02.containsN 3 "abcdef" =! false + + Day02.containsN 2 "bababc" =! true + Day02.containsN 3 "bababc" =! true + + Day02.containsN 2 "abbcde" =! true + Day02.containsN 3 "abbcde" =! false + + Day02.containsN 2 "abcccd" =! false + Day02.containsN 3 "abcccd" =! true + + Day02.containsN 2 "aabcdd" =! true + Day02.containsN 3 "aabcdd" =! false + + Day02.containsN 2 "abcdee" =! true + Day02.containsN 3 "abcdee" =! false + + Day02.containsN 2 "ababab" =! false + Day02.containsN 3 "ababab" =! true + + Day02.parseInput "abcdef, bababc, abbcde, abcccd, aabcdd, abcdee, ababab" |> Day02.checksum =! 12 + + [] + let ``(Part2) From web page`` () = + Day02.commonChars "abcde" "fghij" =! "" + Day02.commonChars "abcde" "axcye" =! "ace" + Day02.commonChars "fghij" "fguij" =! "fgij" + Day02.findTwoStingsWithCommonChars (Day02.parseInput "abcde, fghij, klmno, pqrst, fguij, axcye, wvxyz") 4 =! "fgij" + diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 6458ea4..44bb5aa 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -1,23 +1,18 @@ - netcoreapp2.1 - false - + + + + - - - - - - - + \ No newline at end of file