\r
open System\r
\r
-let diffMajMin = int 'A' - int 'a'\r
+let diffMajMin = int 'a' - int 'A'\r
\r
-let reduce (polymer : string) : string =\r
- Seq.foldBack (\r
+let reduce (polymer : char list) : char list =\r
+ List.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
+let findShortestPolymer (polymer : char list) : char list =\r
+ polymer\r
+ |> List.map Char.ToLower\r
+ |> List.distinct\r
+ |> List.map (fun unit -> polymer |> List.filter (fun c -> Char.ToLower c <> unit) |> reduce)\r
+ |> List.minBy List.length\r
sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime)\r
\r
let day05 () =\r
- let reduced = (File.ReadAllText "Data/day05.input").Trim () |> Day05.reduce\r
+ let reduced = (File.ReadAllText "Data/day05.input").Trim () |> List.ofSeq |> Day05.reduce\r
sprintf "part1 = %A, part2 = %A" reduced.Length (Day05.findShortestPolymer reduced).Length\r
\r
let days : Map<int, unit -> string> =\r
\r
[<Fact>]\r
let ``(Part1) From web page`` () =\r
- let input = "dabAcCaCBAcCcaDA"\r
- Day05.reduce input =! "dabCBAcaDA"\r
+ let input = List.ofSeq "dabAcCaCBAcCcaDA"\r
+ Day05.reduce input =! List.ofSeq "dabCBAcaDA"\r
\r
[<Fact>]\r
let ``(Part2) From web page`` () =\r
- let input = "dabAcCaCBAcCcaDA"\r
- Day05.findShortestPolymer input =! "daDA"\r
+ let input = List.ofSeq "dabAcCaCBAcCcaDA"\r
+ Day05.findShortestPolymer input =! List.ofSeq "daDA"\r
\r