From 306af61cdd23ebf80f38c1947a806de39123f4ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Wed, 5 Dec 2018 13:48:30 +0100 Subject: [PATCH] A shorter day 5 solution. --- Day05.fs | 19 +++++++++---------- Program.fs | 2 +- Tests/Day05 tests.fs | 8 ++++---- 3 files changed, 14 insertions(+), 15 deletions(-) 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" -- 2.45.2