summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-01-08 10:41:47 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-01-08 10:41:47 -0800
commit3194222de4a191baf4062a78a1da735be73da7d1 (patch)
tree1f3677284ac028688e6febd7aa194431fb998cb6
parentae6798da1c29934146f2e7b08678b7bf7b184b34 (diff)
downloadAdventOfCode2016-3194222de4a191baf4062a78a1da735be73da7d1.tar.gz
solution for day 19
-rw-r--r--day19.fsx15
1 files changed, 15 insertions, 0 deletions
diff --git a/day19.fsx b/day19.fsx
new file mode 100644
index 0000000..f30c140
--- /dev/null
+++ b/day19.fsx
@@ -0,0 +1,15 @@
+open System
+
+let rec josephus = 
+    function
+        | 1 -> 1
+        | x  -> (if x % 2 <> 0 then 1 else -1) + 2 * josephus (x / 2)
+
+let josephus' (target: int): int =
+    let pow = Math.Pow(3, Math.Floor(Math.Log(target, 3))) |> int
+    if pow = target then pow
+    else if pow >= target / 2 then target - pow
+    else pow + (2 * (target - 2 * pow))
+
+josephus 3017957 |> printfn "%d"
+josephus' 3017957  |> printfn "%d"
\ No newline at end of file