aboutsummaryrefslogtreecommitdiff
path: root/day03/part1
diff options
context:
space:
mode:
Diffstat (limited to 'day03/part1')
-rwxr-xr-xday03/part119
1 files changed, 19 insertions, 0 deletions
diff --git a/day03/part1 b/day03/part1
new file mode 100755
index 0000000..609a287
--- /dev/null
+++ b/day03/part1
@@ -0,0 +1,19 @@
+#!/usr/bin/env ruby
+
+input = gets.chomp.to_i
+
+n = 0
+n += 1 while ((n*2)+1)**2 < input
+
+s = ((n*2)+1)**2
+
+d = s
+d -= n
+d -= 2*n while d > input
+d += 2*n
+
+if d - n <= input then
+ puts d - input + n
+else
+ puts input - (d - 2*n) + n
+end