From: Grégory Burri Date: Wed, 5 Dec 2018 12:30:57 +0000 (+0100) Subject: Day 5. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=8940d4c6238e40041417e59cea4b87f1fc50aa0b;p=advent_of_code_2018.git Day 5. --- diff --git a/Day05.fs b/Day05.fs index f2a0f10..accb3f9 100644 --- a/Day05.fs +++ b/Day05.fs @@ -1,7 +1,21 @@ module AdventOfCode2018.Day05 open System -open System.Collections.Generic -let parseInput (str : string) : string = - "" +let diffMajMin = int 'A' - int 'a' + +let reduce (polymer : string) : string = + Seq.foldBack ( + fun a result -> + match result with + | b :: rest when int a - int b |> abs = diffMajMin -> rest + | _ -> a :: result + ) polymer [ ] + |> Array.ofList + |> String.Concat + +let findShortestPolymer (polymer : string) : string = + polymer.ToLower() + |> Seq.distinct + |> Seq.map (fun unit -> polymer.Replace(string unit, "", StringComparison.InvariantCultureIgnoreCase) |> reduce) + |> Seq.minBy String.length diff --git a/Program.fs b/Program.fs index 418610f..2ff2dd3 100644 --- a/Program.fs +++ b/Program.fs @@ -22,8 +22,8 @@ let day04 () = sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime) let day05 () = - let foo = File.ReadAllText "Data/day05.input" |> Day05.parseInput - sprintf "part1 = , part2 = " + let reduced = (File.ReadAllText "Data/day05.input").Trim () |> Day05.reduce + sprintf "part1 = %A, part2 = %A" reduced.Length (Day05.findShortestPolymer reduced).Length let days : Map string> = [ diff --git a/Tests/Day05 tests.fs b/Tests/Day05 tests.fs index aa0794c..6c47069 100644 --- a/Tests/Day05 tests.fs +++ b/Tests/Day05 tests.fs @@ -12,9 +12,11 @@ type ``Day05 tests`` (output : ITestOutputHelper) = [] let ``(Part1) From web page`` () = - () + let input = "dabAcCaCBAcCcaDA" + Day05.reduce input =! "dabCBAcaDA" [] let ``(Part2) From web page`` () = - () + let input = "dabAcCaCBAcCcaDA" + Day05.findShortestPolymer input =! "daDA"