From f6d103e37c377b411c25cdedfa6f9bcaa9521f7c Mon Sep 17 00:00:00 2001 From: Ummon Date: Tue, 5 Dec 2017 08:43:03 +0100 Subject: [PATCH] Day 5 --- AdventOfCode2017/AdventOfCode2017.fsproj | 6 +++++- AdventOfCode2017/Day4.fs | 4 +++- AdventOfCode2017/Day5.fs | 19 +++++++++++++++++++ AdventOfCode2017/Program.fs | 5 +++++ Tests/Day5 tests.fs | 17 +++++++++++++++++ Tests/Tests.fsproj | 1 + 6 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 AdventOfCode2017/Day5.fs create mode 100644 Tests/Day5 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index dac598e..5a9b3ef 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 4 + 5 pdbonly @@ -60,6 +60,7 @@ + @@ -71,6 +72,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day4.fs b/AdventOfCode2017/Day4.fs index c71ec77..fdab92f 100644 --- a/AdventOfCode2017/Day4.fs +++ b/AdventOfCode2017/Day4.fs @@ -7,7 +7,9 @@ let forallDistinctPairs (f : string -> string -> bool) (pp : string) = for b in a + 1 .. words.Length - 1 -> f words.[a] words.[b] ] |> List.forall not +let isAnagram w1 w2 = Seq.compareWith compare (Seq.sort w1) (Seq.sort w2) = 0 + let passphraseValid = forallDistinctPairs (=) -let isAnagram w1 w2 = Seq.compareWith (compare) (Seq.sort w1) (Seq.sort w2) = 0 let passphraseValidAnagram = forallDistinctPairs isAnagram + let nbPassphrasesValid (f : string -> bool) = Seq.map f >> Seq.sumBy (fun v -> if v then 1 else 0) \ No newline at end of file diff --git a/AdventOfCode2017/Day5.fs b/AdventOfCode2017/Day5.fs new file mode 100644 index 0000000..dea50e3 --- /dev/null +++ b/AdventOfCode2017/Day5.fs @@ -0,0 +1,19 @@ +module AdventOfCode2017.Day5 + +open System + +let parseInput (str : string) : int[] = + str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + +let nbSteps (next : int -> int) (instructions : int[]) = + let is = instructions.[*] + let mutable cursor, steps = 0, 0 + while cursor >= 0 && cursor < instructions.Length do + let i = is.[cursor] + is.[cursor] <- next is.[cursor] + cursor <- cursor + i + steps <- steps + 1 + steps + +let nbSteps1 = nbSteps ((+) 1) +let nbSteps2 = nbSteps (fun i -> if i >= 3 then i - 1 else i + 1) \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 52ac0e9..db1ca80 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -19,6 +19,10 @@ 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 day5 () = + let input = File.ReadAllText "Data/day5.input" + sprintf "part1 = %A, part2 = %A" (Day5.nbSteps1 (Day5.parseInput input)) (Day5.nbSteps2 (Day5.parseInput input)) + let doDay (n : int) = let result = match n with @@ -26,6 +30,7 @@ let doDay (n : int) = | 2 -> day2 () | 3 -> day3 () | 4 -> day4 () + | 5 -> day5 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s" n result diff --git a/Tests/Day5 tests.fs b/Tests/Day5 tests.fs new file mode 100644 index 0000000..3ddf954 --- /dev/null +++ b/Tests/Day5 tests.fs @@ -0,0 +1,17 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day5 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day5.nbSteps1 [| 0; 3; 0; 1; -3 |] =! 5 + + [] + let ``(Part2) From web page`` () = + Day5.nbSteps2 [| 0; 3; 0; 1; -3 |] =! 10 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 9721c8e..1b36559 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -59,6 +59,7 @@ + -- 2.45.2