Skip to content

Instantly share code, notes, and snippets.

@tariknz
Created December 5, 2018 09:27
Show Gist options
  • Save tariknz/3c6b0f03b73d2411e9ec9586e7f746c7 to your computer and use it in GitHub Desktop.
Save tariknz/3c6b0f03b73d2411e9ec9586e7f746c7 to your computer and use it in GitHub Desktop.
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