diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:15:20 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:15:20 +0100 |
| commit | 03f28cae3ddea3b08a671dd3f20f3d32777aa4db (patch) | |
| tree | 7aea1f86e706cf8233d89d704ac2342a0c63d059 /challenge-109/abigail/ruby | |
| parent | 46b8aecc9397c6211a1e97a7f0638833726294a2 (diff) | |
| parent | 1ff197d81f941c3dd35d77bec8a0326807e8d2b1 (diff) | |
| download | perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.gz perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.bz2 perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-109/abigail/ruby')
| -rw-r--r-- | challenge-109/abigail/ruby/ch-1.rb | 44 | ||||
| -rw-r--r-- | challenge-109/abigail/ruby/ch-2.rb | 48 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-109/abigail/ruby/ch-1.rb b/challenge-109/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..0c73c368dd --- /dev/null +++ b/challenge-109/abigail/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb [plain | compute] +# + +PLAIN = 0 +COMPUTE = 1 + +COUNT = 20 + +def divisor_sum (n) + sum = 0 + for i in 2 .. n / 2 + if n % i == 0 + then sum += i + end + end + return sum +end + +type = PLAIN + +if ARGV . length > 0 && ARGV[0] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then puts ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"); +end + +if type == COMPUTE +then for n in 1 .. COUNT + if n > 1 + then print (", ") + end + print divisor_sum (n) + end + puts ("") +end diff --git a/challenge-109/abigail/ruby/ch-2.rb b/challenge-109/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..faaa7b8489 --- /dev/null +++ b/challenge-109/abigail/ruby/ch-2.rb @@ -0,0 +1,48 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +SIZE = 7 + +# +# Brute force all possibilities, with early returns. +# +ARGF . each_line do |_| + numbers = (_ . split) . map {|_| _ . to_i} + + numbers . each_with_index do |a, a_i| + numbers . each_with_index do |b, b_i| + next if a_i == b_i + target = a + b + numbers . each_with_index do |c, c_i| + next if a_i == c_i || b_i == c_i + numbers . each_with_index do |d, d_i| + next if a_i == d_i || b_i == d_i || c_i == d_i + next if target != b + c + d + numbers . each_with_index do |e, e_i| + next if a_i == e_i || b_i == e_i || c_i == e_i || + d_i == e_i + numbers . each_with_index do |f, f_i| + next if a_i == f_i || b_i == f_i || c_i == f_i || + d_i == f_i || e_i == f_i + next if target != d + e + f + numbers . each_with_index do |g, g_i| + next if a_i == g_i || b_i == g_i || + c_i == g_i || d_i == g_i || + e_i == g_i || f_i == g_i + next if target != f + g + puts "#{a} #{b} #{c} #{d} #{e} #{f} #{g}" + end + end + end + end + end + end + end +end |
