diff options
| author | Abigail <abigail@abigail.be> | 2021-05-31 20:16:50 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-06-01 15:38:19 +0200 |
| commit | 4f04dab2323d4219a5eb906c4bbc28059fecb10c (patch) | |
| tree | 17249389d57132215ed5cb3c77b3b7e8a72cb156 /challenge-115/abigail/ruby | |
| parent | 9def9e9e8ab96a506f322adf88fbdb182fd9acb7 (diff) | |
| download | perlweeklychallenge-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.rb | 57 |
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 |
