From 32d788df81ea1d7b4b52ee8d97eff54c2134cf05 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Fri, 7 Jan 2022 21:20:27 -0800 Subject: solution for day 16 --- day16.fsx | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 day16.fsx diff --git a/day16.fsx b/day16.fsx new file mode 100644 index 0000000..adc57ca --- /dev/null +++ b/day16.fsx @@ -0,0 +1,35 @@ +let starter = "01000100010010111" + +let revStr (str: string) = + str + |> Seq.toArray + |> Array.rev + |> Array.map string + |> String.concat "" + +let logical_not str = + String.map (function | '1' -> '0' | '0' -> '1' | _ -> char 0) str + +let rec dragon targetLength inputStr = + let newStr = inputStr + "0" + (inputStr |> revStr |> logical_not) + if newStr.Length >= targetLength + then newStr[..targetLength-1] + else dragon targetLength newStr + +let rec checksum (inputStr: string): string = + let parity (chars: char[]): bool = + let a, b = chars[0], chars[1] + a = b + if inputStr.Length % 2 <> 0 + then inputStr + else + let pairs = Seq.chunkBySize 2 inputStr + |> Seq.map parity + |> Seq.map (fun x -> if x then "1" else "0") + |> String.concat "" + checksum pairs + +// part 1 +starter |> dragon 272 |> checksum |> printfn "%s" +// part 2 +starter |> dragon 35651584 |> checksum |> printfn "%s" -- cgit 1.4.1-2-gfad0