aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-26 22:23:21 +0100
committerAbigail <abigail@abigail.be>2021-02-26 22:23:21 +0100
commit4f82982427c9ee064c87162f8ba457fc95672874 (patch)
treea97f7278a5ff270c224af3a8649826b1a02ad17d /challenge-101/abigail/ruby
parent77b8ddc12a0adf390467890f397dfe2f61f08910 (diff)
downloadperlweeklychallenge-club-4f82982427c9ee064c87162f8ba457fc95672874.tar.gz
perlweeklychallenge-club-4f82982427c9ee064c87162f8ba457fc95672874.tar.bz2
perlweeklychallenge-club-4f82982427c9ee064c87162f8ba457fc95672874.zip
Ruby solution for week 101, part 1
Diffstat (limited to 'challenge-101/abigail/ruby')
-rw-r--r--challenge-101/abigail/ruby/ch-1.rb111
1 files changed, 111 insertions, 0 deletions
diff --git a/challenge-101/abigail/ruby/ch-1.rb b/challenge-101/abigail/ruby/ch-1.rb
new file mode 100644
index 0000000000..4a383ab873
--- /dev/null
+++ b/challenge-101/abigail/ruby/ch-1.rb
@@ -0,0 +1,111 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1.rb < input-file
+#
+
+RIGHT = 0
+UP = 1
+LEFT = 2
+DOWN = 3
+
+#
+# Read the data, one line of input, split on whitespace.
+#
+elements = gets . chomp . split
+
+#
+# Calculate the optimal height and width
+#
+count = elements . length
+height = (Math . sqrt (count)) . floor
+while count % height > 0
+ height -= 1
+end
+width = count / height
+
+#
+# Initialize the matrix
+#
+matrix = Array . new (height)
+for x in 0 .. height - 1 do
+ matrix [x] = Array . new (width)
+end
+
+
+#
+# Fill the matrix, starting from the bottom left.
+#
+min_x = 0
+max_x = height - 1
+min_y = 0
+max_y = width - 1
+x = max_x
+y = min_y
+direction = RIGHT
+
+for element in elements do
+ matrix [x] [y] = element
+ turn = false
+ if direction == RIGHT
+ if y >= max_y then turn = true; x -= 1; max_x -= 1
+ else y += 1
+ end
+ end
+
+ if direction == UP
+ if x <= min_x then turn = true; y -= 1; max_y -= 1
+ else x -= 1
+ end
+ end
+
+ if direction == LEFT
+ if y <= min_y then turn = true; x += 1; min_x += 1
+ else y -= 1
+ end
+ end
+
+ if direction == DOWN
+ if x >= max_x then turn = true; y += 1; min_y += 1
+ else x += 1
+ end
+ end
+
+ if turn
+ direction += 1
+ direction %= 4
+ end
+
+end
+
+#
+# Create an array with column widths
+#
+widths = Array . new (width)
+for y in 0 .. width - 1 do
+ max = 0
+ for x in 0 .. height - 1 do
+ if max < matrix [x] [y] . length
+ max = matrix [x] [y] . length
+ end
+ end
+ widths [y] = max
+end
+
+#
+# Pretty print the matrix
+#
+for x in 0 .. height - 1 do
+ for y in 0 .. width - 1 do
+ if y > 0
+ print " "
+ end
+ format = "%" + widths [y] . to_s + "s"
+ printf format, matrix [x] [y]
+ end
+ print "\n"
+end