aboutsummaryrefslogtreecommitdiff
path: root/challenge-115/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-31 20:16:50 +0200
committerAbigail <abigail@abigail.be>2021-06-01 15:38:19 +0200
commit4f04dab2323d4219a5eb906c4bbc28059fecb10c (patch)
tree17249389d57132215ed5cb3c77b3b7e8a72cb156 /challenge-115/abigail/ruby
parent9def9e9e8ab96a506f322adf88fbdb182fd9acb7 (diff)
downloadperlweeklychallenge-club-4f04dab2323d4219a5eb906c4bbc28059fecb10c.tar.gz
perlweeklychallenge-club-4f04dab2323d4219a5eb906c4bbc28059fecb10c.tar.bz2
perlweeklychallenge-club-4f04dab2323d4219a5eb906c4bbc28059fecb10c.zip
AWK, Bash, C, Lua, Node.js, Perl, Python and Ruby solutions for week 115, part 2
Diffstat (limited to 'challenge-115/abigail/ruby')
-rw-r--r--challenge-115/abigail/ruby/ch-2.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-115/abigail/ruby/ch-2.rb b/challenge-115/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..e1e41a91fd
--- /dev/null
+++ b/challenge-115/abigail/ruby/ch-2.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+nr_of_digits = 10
+
+ARGF . each_line do
+ |line|
+ #
+ # Read the input and count the digits
+ #
+ digits = []
+ for d in 0 .. nr_of_digits - 1 do
+ digits [d] = 0
+ end
+ line . split() . each do
+ |d|
+ digits [d . to_i] += 1
+ end
+
+ #
+ # Find the lowest even number
+ #
+ last = -1
+ for i in 1 .. nr_of_digits do
+ d = nr_of_digits - i
+ if d % 2 == 0 && digits [d] > 0
+ then last = d
+ end
+ end
+
+ #
+ # Skip if the input does not contain an even number
+ #
+ if last < 0
+ then next
+ end
+
+ digits [last] -= 1
+
+ #
+ # Print the digits, highest to lowest
+ #
+ for i in 1 .. nr_of_digits do
+ d = nr_of_digits - i
+ for j in 1 .. digits [d] do
+ print (d)
+ end
+ end
+ puts (last)
+end