2018-12-05 - Alchemical Reduction
(original .ipynb)open System.IO let realInput = File.ReadAllText("puzzle_input/day5.txt") let testInput = "dabAcCaCBAcCcaDA"
let (=?) s1 s2 = System.String.Equals(s1, s2, System.StringComparison.CurrentCultureIgnoreCase) && not (s1.Equals(s2)) let (=??) s1 s2 = System.String.Equals(s1, s2, System.StringComparison.CurrentCultureIgnoreCase) let stringToList (str:string) = str.ToCharArray() |> Array.map (fun c -> c.ToString()) |> Array.toList let rec reducePolymer' polymerList = match polymerList with | [] -> [] | u::[] -> [ u ] | u::v::rest -> if u =? v then reducePolymer' rest else u :: (reducePolymer' (v :: rest)) let rec reducePolymer (polymerString:string) = let polymerList = stringToList polymerString let reactedPolymerString = reducePolymer' polymerList |> String.concat "" if polymerString.Length = reactedPolymerString.Length then reactedPolymerString else reducePolymer reactedPolymerString // Part 1 (reducePolymer realInput).Length |> printfn "part one: %A" // Part 2 let reduceMinusUnit polymerList ch = polymerList |> List.filter (fun c -> not (c =?? ch)) |> String.concat "" |> reducePolymer // surely a better way to do this let allLetters = " abcdefghijklmnopqrstuvwxyz" stringToList allLetters |> List.map (fun c -> (c, (reduceMinusUnit (stringToList realInput) c).Length )) |> List.sortBy (fun p -> snd p) |> List.head |> snd |> printfn "part two: %A"
part one: 10496 part two: 5774