Created
December 5, 2018 09:27
-
-
Save tariknz/3c6b0f03b73d2411e9ec9586e7f746c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.IO | |
open System | |
let input = File.ReadLines("2018-day-5.txt") | |
let rec polymerExploder input: string = | |
let polymerSome = | |
input | |
|> Seq.toArray | |
|> Seq.pairwise | |
|> Seq.tryFind (fun (a, b) -> a <> b && Char.ToLower(a) = Char.ToLower(b)) | |
if polymerSome.IsNone then input else | |
let polymer = polymerSome.Value |> (fun (a, b) -> string a + string b) | |
let updatedSequence = input.Replace(polymer, "") | |
polymerExploder updatedSequence | |
// part 1 | |
input | |
|> Seq.head | |
|> polymerExploder | |
|> Seq.length | |
|> printfn "%A" | |
// part 2 | |
let replacedInput c input = | |
input | |
|> Seq.head | |
|> fun s -> (string s).Replace((string c).ToLower(), "") | |
|> fun s -> (string s).Replace((string c).ToUpper(), "") | |
['a' .. 'z'] | |
|> Seq.map (fun c -> c, replacedInput c input |> polymerExploder |> Seq.length) | |
|> Seq.sortBy snd | |
|> Seq.head | |
|> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment