summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--day16.fsx35
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"