open System\r
\r
let parseInput (str : string) : int[] =\r
- str.Split ([| "\r\n"; "\r"; "\n" |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
+ str.Split ([| "\r\n"; "\r"; "\n"; "," |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
\r
let finalFrequency : seq<int> -> int =\r
Seq.sum\r
else\r
(false, sum', Set.add sum' frequencies)\r
\r
- ) (false, 0, Set.empty) repeatedChanges\r
+ ) (false, 0, Set.ofList [ 0 ]) repeatedChanges\r
|> Seq.pick (fun (duplicate, sum, _) -> if duplicate then Some sum else None)\r
open System\r
\r
let parseInput (str : string) : string[] =\r
- str.Split ([| "\r\n"; "\r"; "\n" |], StringSplitOptions.RemoveEmptyEntries)\r
+ str.Split ([| "\r\n"; "\r"; "\n"; ","; " " |], StringSplitOptions.RemoveEmptyEntries)\r
\r
-let containsN (id : string) : bool =\r
- false\r
+let containsN (n : int) (id : string) : bool = \r
+ id |> Seq.map (fun c -> id |> Seq.filter ((=) c) |> Seq.length) |> Seq.contains n\r
\r
+let checksum (ids : string[]) : int =\r
+ let nbIds (n : int) = ids |> Seq.filter (containsN n) |> Seq.length\r
+ nbIds 2 * nbIds 3\r
+\r
+let commonChars (a : string) (b : string) : string =\r
+ Seq.zip a b |> Seq.choose (fun (c1, c2) -> if c1 = c2 then Some c1 else None) |> String.Concat\r
+\r
+let findTwoStingsWithCommonChars (l : string array) (n : int) : string = \r
+ let rec find i =\r
+ match \r
+ (l.[i..] |> Array.tryPick (\r
+ fun str -> \r
+ let common = commonChars l.[i] str\r
+ if common.Length = n then Some common else None\r
+ )) with\r
+ | Some found -> found\r
+ | None -> find (i + 1)\r
+ find 0\r
sprintf "part1 = %A, part2 = %A" (Day01.finalFrequency changes) (Day01.firstDuplicate changes)\r
\r
let day02 () =\r
- //let input = File.ReadAllText "Data/day02.input" |> Day01.parseInput\r
- "day02"\r
+ let ids = File.ReadAllText "Data/day02.input" |> Day02.parseInput\r
+ let idLength = ids.[0].Length\r
+ sprintf "part1 = %A, part2 = %A" (Day02.checksum ids) (Day02.findTwoStingsWithCommonChars ids (idLength - 1))\r
\r
let days : (unit -> string) array =\r
[|\r
-module AdventOfCode2018.Tests\r
+namespace AdventOfCode2018.Tests\r
\r
open System\r
\r
type ``Day01 tests`` (output : ITestOutputHelper) =\r
\r
[<Fact>]\r
- let ``(Part1) My test`` () =\r
- 1 =! 2\r
+ let ``(Part1) From web page`` () =\r
+ Day01.parseInput "+1, +1, +1" |> Day01.finalFrequency =! 3\r
+ Day01.parseInput "+1, +1, -2" |> Day01.finalFrequency =! 0\r
+ Day01.parseInput "-1, -2, -3" |> Day01.finalFrequency =! -6\r
+ \r
+ [<Fact>]\r
+ let ``(Part2) From web page`` () =\r
+ Day01.parseInput "+1, -1" |> Day01.firstDuplicate =! 0\r
+ Day01.parseInput "+3, +3, +4, -2, -4" |> Day01.firstDuplicate =! 10\r
+ Day01.parseInput "-6, +3, +8, +5, -6" |> Day01.firstDuplicate =! 5\r
+ Day01.parseInput "+7, +7, -2, -7, -4" |> Day01.firstDuplicate =! 14\r
+\r
--- /dev/null
+namespace AdventOfCode2018.Tests\r
+\r
+open System\r
+\r
+open Xunit\r
+open Xunit.Abstractions\r
+open Swensen.Unquote\r
+\r
+open AdventOfCode2018\r
+\r
+type ``Day02 tests`` (output : ITestOutputHelper) =\r
+\r
+ [<Fact>]\r
+ let ``(Part1) From web page`` () =\r
+ Day02.containsN 2 "abcdef" =! false\r
+ Day02.containsN 3 "abcdef" =! false\r
+\r
+ Day02.containsN 2 "bababc" =! true\r
+ Day02.containsN 3 "bababc" =! true\r
+\r
+ Day02.containsN 2 "abbcde" =! true\r
+ Day02.containsN 3 "abbcde" =! false\r
+\r
+ Day02.containsN 2 "abcccd" =! false\r
+ Day02.containsN 3 "abcccd" =! true\r
+\r
+ Day02.containsN 2 "aabcdd" =! true\r
+ Day02.containsN 3 "aabcdd" =! false\r
+\r
+ Day02.containsN 2 "abcdee" =! true\r
+ Day02.containsN 3 "abcdee" =! false\r
+\r
+ Day02.containsN 2 "ababab" =! false\r
+ Day02.containsN 3 "ababab" =! true\r
+\r
+ Day02.parseInput "abcdef, bababc, abbcde, abcccd, aabcdd, abcdee, ababab" |> Day02.checksum =! 12\r
+\r
+ [<Fact>]\r
+ let ``(Part2) From web page`` () =\r
+ Day02.commonChars "abcde" "fghij" =! ""\r
+ Day02.commonChars "abcde" "axcye" =! "ace"\r
+ Day02.commonChars "fghij" "fguij" =! "fgij" \r
+ Day02.findTwoStingsWithCommonChars (Day02.parseInput "abcde, fghij, klmno, pqrst, fguij, axcye, wvxyz") 4 =! "fgij" \r
+\r
<Project Sdk="Microsoft.NET.Sdk">\r
-\r
<PropertyGroup>\r
<TargetFramework>netcoreapp2.1</TargetFramework>\r
- <IsPackable>false</IsPackable>\r
</PropertyGroup>\r
-\r
<ItemGroup>\r
+ <ProjectReference Include="..\AdventOfCode2018.fsproj" />\r
+ </ItemGroup>\r
+ <ItemGroup>\r
+ <Compile Include="Day02 tests.fs" />\r
<Compile Include="Day01 tests.fs" />\r
</ItemGroup>\r
-\r
<ItemGroup>\r
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />\r
<PackageReference Include="Unquote" Version="4.0.0" />\r
<PackageReference Include="xunit" Version="2.4.0" />\r
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />\r
</ItemGroup>\r
-\r
- <ItemGroup>\r
- <ProjectReference Include="..\AdventOfCode2018.fsproj" />\r
- </ItemGroup>\r
-\r
-</Project>\r
+</Project>
\ No newline at end of file