From: Grégory Burri Date: Wed, 5 Dec 2018 12:48:30 +0000 (+0100) Subject: A shorter day 5 solution. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=306af61cdd23ebf80f38c1947a806de39123f4ba;p=advent_of_code_2018.git A shorter day 5 solution. --- diff --git a/Day05.fs b/Day05.fs index accb3f9..a752d91 100644 --- a/Day05.fs +++ b/Day05.fs @@ -2,20 +2,19 @@ module AdventOfCode2018.Day05 open System -let diffMajMin = int 'A' - int 'a' +let diffMajMin = int 'a' - int 'A' -let reduce (polymer : string) : string = - Seq.foldBack ( +let reduce (polymer : char list) : char list = + List.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 +let findShortestPolymer (polymer : char list) : char list = + polymer + |> List.map Char.ToLower + |> List.distinct + |> List.map (fun unit -> polymer |> List.filter (fun c -> Char.ToLower c <> unit) |> reduce) + |> List.minBy List.length diff --git a/Program.fs b/Program.fs index 2ff2dd3..fb433b7 100644 --- a/Program.fs +++ b/Program.fs @@ -22,7 +22,7 @@ let day04 () = sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime) let day05 () = - let reduced = (File.ReadAllText "Data/day05.input").Trim () |> Day05.reduce + let reduced = (File.ReadAllText "Data/day05.input").Trim () |> List.ofSeq |> 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 6c47069..560db29 100644 --- a/Tests/Day05 tests.fs +++ b/Tests/Day05 tests.fs @@ -12,11 +12,11 @@ type ``Day05 tests`` (output : ITestOutputHelper) = [] let ``(Part1) From web page`` () = - let input = "dabAcCaCBAcCcaDA" - Day05.reduce input =! "dabCBAcaDA" + let input = List.ofSeq "dabAcCaCBAcCcaDA" + Day05.reduce input =! List.ofSeq "dabCBAcaDA" [] let ``(Part2) From web page`` () = - let input = "dabAcCaCBAcCcaDA" - Day05.findShortestPolymer input =! "daDA" + let input = List.ofSeq "dabAcCaCBAcCcaDA" + Day05.findShortestPolymer input =! List.ofSeq "daDA"