summary refs log tree commit diff stats
path: root/day15.fsx
diff options
context:
space:
mode:
Diffstat (limited to 'day15.fsx')
-rw-r--r--day15.fsx34
1 files changed, 34 insertions, 0 deletions
diff --git a/day15.fsx b/day15.fsx
new file mode 100644
index 0000000..6c7d977
--- /dev/null
+++ b/day15.fsx
@@ -0,0 +1,34 @@
+open System
+
+type Generator =
+    struct
+        val mutable current: int64
+        val factor: int64
+        new (c, f) = {current=c; factor=f}
+
+        member this.generate(multiple: int64) =
+            this.current <- (this.current * this.factor) % 2147483647L
+            if multiple <> 0 && this.current % multiple <> 0 then this.generate(multiple)
+        
+        member this.toBits() =
+            Convert.ToString(this.current, 2).PadLeft(32, '0') |> Seq.toArray |> (fun x -> x[16..])
+    end
+
+let countValid iterations multipleA multipleB =
+    let mutable generatorA = new Generator(591, 16807)
+    let mutable generatorB = new Generator(393, 48271)
+    
+    let mutable count = 0
+    for i = 1 to iterations do
+        generatorA.generate(multipleA)
+        generatorB.generate(multipleB)
+        let aLow16 = generatorA.toBits()
+        let bLow16 = generatorB.toBits()
+        if aLow16 = bLow16 then 
+            count <- count + 1
+    
+    printfn "%d" count
+
+let () =
+    countValid 40000000 0 0
+    countValid 5000000 4 8
\ No newline at end of file