summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-02-14 22:16:43 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-02-14 22:16:43 -0800
commitf3d54ee9e83324da6c2c8fc90ea2b5d5d749b0a5 (patch)
treefb7e5ce40163ad66018a563df4a3df371c77d220
parentd520f8941d8fb63e70d19ea5bbc9246641ef7593 (diff)
downloadAdventOfCode2017-f3d54ee9e83324da6c2c8fc90ea2b5d5d749b0a5.tar.gz
solution for day 10
-rw-r--r--day10.fsx39
1 files changed, 39 insertions, 0 deletions
diff --git a/day10.fsx b/day10.fsx
new file mode 100644
index 0000000..da236d3
--- /dev/null
+++ b/day10.fsx
@@ -0,0 +1,39 @@
+open System.IO
+
+let revSublist nums pos n =
+    let c = Array.copy nums
+    for i in [0..n-1] do
+        nums.[(pos + i) % 256] <- c.[(pos + n - i - 1) % 256]
+    
+let hash nRounds input =
+    let nums = [|0..255|]
+    let mutable pos = 0
+    let mutable skipsize = 0
+
+    for _ in [1..nRounds] do 
+        for n in input do
+            revSublist nums pos n
+            pos <- pos + n + skipsize
+            skipsize <- skipsize + 1
+    nums
+
+let part1 (input: string) =
+    input.Split(',')
+    |> Array.map int
+    |> hash 1
+    |> Array.take 2
+    |> Array.reduce (*)
+
+let part2 (input: string) =
+    input
+    |> Seq.map (fun c -> byte c)
+    |> (fun a -> Seq.append a [|17uy; 31uy; 73uy; 47uy; 23uy|])
+    |> Seq.map int
+    |> hash 64
+    |> Array.chunkBySize 16
+    |> Array.map (Array.reduce (^^^))
+    |> Array.fold (fun str digit -> str + sprintf "%02x" digit) ""
+
+let lengths = File.ReadAllText "day10.txt"
+part1 lengths |> printfn "%d"
+part2 lengths |> printfn "%s"
\ No newline at end of file