summary refs log tree commit diff stats
path: root/day3.ml
diff options
context:
space:
mode:
Diffstat (limited to 'day3.ml')
-rw-r--r--day3.ml24
1 files changed, 24 insertions, 0 deletions
diff --git a/day3.ml b/day3.ml
new file mode 100644
index 0000000..bcca156
--- /dev/null
+++ b/day3.ml
@@ -0,0 +1,24 @@
+open Float
+
+let input = 265149
+
+let coordinates n =
+    let k = int_of_float (ceil (((sqrt (float_of_int n)) -. 1.) /. 2.)) in
+    let t = 1 + (2 * k) in
+    let m = int_of_float ((float_of_int t) ** 2.) in
+    let t = t - 1 in
+    if n >= m - t then
+        (k - (m - n), -k)
+    else if n >= m - (2 * t) then
+        (-k, -k + (m - n))
+    else if n >= m - (3 * t) then
+        (-k + (m - n), k)
+    else
+        (k, k - (m - n - t))
+
+let () =
+    let (x, y) = coordinates input in
+        Printf.printf "%d\n" (Int.abs x + Int.abs y)
+
+        
+(* Part 2 is listed on OEIS *)