diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-01-07 21:20:27 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-01-07 21:20:27 -0800 |
commit | 32d788df81ea1d7b4b52ee8d97eff54c2134cf05 (patch) | |
tree | c414ac728534240a44ccdd80082dc9d180e9da51 | |
parent | 465484d81e536596b544d8f1c8f1c61dd3fe49b5 (diff) | |
download | AdventOfCode2016-32d788df81ea1d7b4b52ee8d97eff54c2134cf05.tar.gz |
solution for day 16
-rw-r--r-- | day16.fsx | 35 |
1 files changed, 35 insertions, 0 deletions
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" |