From b5e8f96e5d6dedb6b55515a9dca8b70d9d3375ee Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Fri, 18 Feb 2022 20:42:00 -0800 Subject: solution for day 15 --- day15.fsx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day15.fsx 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 -- cgit 1.4.1-2-gfad0