module AdventOfCode2018.Day05\r
\r
open System\r
-open System.Collections.Generic\r
\r
-let parseInput (str : string) : string =\r
- ""\r
+let diffMajMin = int 'A' - int 'a'\r
+\r
+let reduce (polymer : string) : string =\r
+ Seq.foldBack (\r
+ fun a result ->\r
+ match result with\r
+ | b :: rest when int a - int b |> abs = diffMajMin -> rest\r
+ | _ -> a :: result\r
+ ) polymer [ ]\r
+ |> Array.ofList\r
+ |> String.Concat\r
+\r
+let findShortestPolymer (polymer : string) : string =\r
+ polymer.ToLower()\r
+ |> Seq.distinct\r
+ |> Seq.map (fun unit -> polymer.Replace(string unit, "", StringComparison.InvariantCultureIgnoreCase) |> reduce)\r
+ |> Seq.minBy String.length\r
sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime)\r
\r
let day05 () =\r
- let foo = File.ReadAllText "Data/day05.input" |> Day05.parseInput\r
- sprintf "part1 = , part2 = "\r
+ let reduced = (File.ReadAllText "Data/day05.input").Trim () |> Day05.reduce\r
+ sprintf "part1 = %A, part2 = %A" reduced.Length (Day05.findShortestPolymer reduced).Length\r
\r
let days : Map<int, unit -> string> =\r
[\r
\r
[<Fact>]\r
let ``(Part1) From web page`` () =\r
- ()\r
+ let input = "dabAcCaCBAcCcaDA"\r
+ Day05.reduce input =! "dabCBAcaDA"\r
\r
[<Fact>]\r
let ``(Part2) From web page`` () =\r
- ()\r
+ let input = "dabAcCaCBAcCcaDA"\r
+ Day05.findShortestPolymer input =! "daDA"\r
\r