diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 19:14:23 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 19:14:23 +0100 |
| commit | 922b81eeb7516829d0311fd3fb5b9fa51fe48876 (patch) | |
| tree | a7b1a94a9933755768797dcfbf7b946d2da79679 | |
| parent | c6bb015992c65d4248161ca12c22a1af6dd29711 (diff) | |
| download | perlweeklychallenge-club-922b81eeb7516829d0311fd3fb5b9fa51fe48876.tar.gz perlweeklychallenge-club-922b81eeb7516829d0311fd3fb5b9fa51fe48876.tar.bz2 perlweeklychallenge-club-922b81eeb7516829d0311fd3fb5b9fa51fe48876.zip | |
Week 151: Ruby solutions
| -rw-r--r-- | challenge-151/abigail/ruby/ch-1.rb | 31 | ||||
| -rw-r--r-- | challenge-151/abigail/ruby/ch-2.rb | 17 |
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-151/abigail/ruby/ch-1.rb b/challenge-151/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..1164c5bdfc --- /dev/null +++ b/challenge-151/abigail/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |line| + tree = line . strip() . split(/\|/) . map do |row| + row . strip() . split(/ +/) . map do |x| + x == "*" ? false : true + end + end + done = false + for d in 0 .. tree . length() - 1 do + for i in 0 .. tree [d] . length() - 1 do + if tree [d] [i] && !tree [d + 1] [2 * i] && + !tree [d + 1] [2 * i + 1] then + puts (d + 1) + done = true + break + end + end + if done then + break + end + end +end diff --git a/challenge-151/abigail/ruby/ch-2.rb b/challenge-151/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..ebdd805af5 --- /dev/null +++ b/challenge-151/abigail/ruby/ch-2.rb @@ -0,0 +1,17 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |line| + (h = line . split . map do |n| n . to_i end) . push(0, 0) + (h . length - 3) . downto(2) do |i| + h[i] = [h[i] + h[i + 2], h[i + 1]] . max + end + puts (h[0] + h[2]) +end |
