summary refs log tree commit diff stats
path: root/day9.fsx
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-01-04 23:54:17 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-01-04 23:54:17 -0800
commitc377501ad97ce336c52a04ae9bf85ca335b814ba (patch)
treed2bbef0e54fa8328a1649862eaf0253981363753 /day9.fsx
parent102086e040577a4f6b3e7d97e442ee32e12f3e27 (diff)
downloadAdventOfCode2016-c377501ad97ce336c52a04ae9bf85ca335b814ba.tar.gz
solution for day 9
Diffstat (limited to 'day9.fsx')
-rw-r--r--day9.fsx36
1 files changed, 36 insertions, 0 deletions
diff --git a/day9.fsx b/day9.fsx
new file mode 100644
index 0000000..de64fea
--- /dev/null
+++ b/day9.fsx
@@ -0,0 +1,36 @@
+open System.IO
+open System.Text.RegularExpressions
+
+let (|Regex|_|) input =
+    let m = Regex.Match(input, "^(?:\((\w+?)\))(.*)")
+    if m.Success then Some(List.tail [for g in m.Groups -> g.Value])
+    else None
+
+let splitPattern str =
+    match str with 
+    | Regex [marker; rest] -> marker, rest
+    | _ -> "", ""
+
+let rec decrypt str =
+    if str = "" then ""
+    else
+        let marker, rest = splitPattern str
+        if marker = "" then string str[0] + decrypt str[1..]
+        else
+            let nums = marker.Split 'x' |> Array.map int
+            let start, count = (min nums[0] (String.length rest)), nums[1]
+            String.replicate count rest[..start-1] + decrypt rest[start..]
+
+let rec decryptLength str =
+    if str = "" then bigint 0
+    else
+        let marker, rest = splitPattern str
+        if marker = "" then bigint 1 + decryptLength str[1..]
+        else
+            let nums = marker.Split 'x' |> Array.map int
+            let start, count = (min nums[0] (String.length rest)), nums[1]
+            bigint count * decryptLength rest[..start-1] + decryptLength rest[start..]
+
+let lines = File.ReadLines "day9.txt"
+lines |> Seq.map decrypt |> Seq.map String.length |> Seq.reduce (+) |> printfn "%d"
+lines |> Seq.map decryptLength |> Seq.reduce (+) |> printfn "%A"