aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-27 19:11:09 +0100
committerAbigail <abigail@abigail.be>2021-01-27 19:11:09 +0100
commitf1dbdec48c9b3d003f7f8eaaec4c6070be6652f6 (patch)
tree593b075325a1a247d7843f3907a33dea18db5651 /challenge-097/abigail/ruby
parent46fdef8d9c84252e197f529b12987980e1cf6ff5 (diff)
downloadperlweeklychallenge-club-f1dbdec48c9b3d003f7f8eaaec4c6070be6652f6.tar.gz
perlweeklychallenge-club-f1dbdec48c9b3d003f7f8eaaec4c6070be6652f6.tar.bz2
perlweeklychallenge-club-f1dbdec48c9b3d003f7f8eaaec4c6070be6652f6.zip
Bash and Ruby solutions for week 97, part 2
Diffstat (limited to 'challenge-097/abigail/ruby')
-rw-r--r--challenge-097/abigail/ruby/ch-2.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-097/abigail/ruby/ch-2.rb b/challenge-097/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..06607c29d7
--- /dev/null
+++ b/challenge-097/abigail/ruby/ch-2.rb
@@ -0,0 +1,47 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb -s SECTIONS < input-file
+#
+
+require 'optparse'
+
+NR_OF_LETTERS = 26
+
+
+#
+# Parse and validate options
+#
+params = ARGV . getopts ('s:')
+sections = params ["s"] ? params ["s"] . to_i : -1
+
+if sections < 0
+ STDERR . puts "Requires a -s SECTIONS option"
+ exit 1
+end
+
+
+#
+# Iterate over the input. For each position, count the number of 0s,
+# and calculate the number of 1s. Sum the minimum of those numbers.
+#
+ARGF . each_line do |line|
+ s_len = (line . length - 1) / sections
+ sum = 0
+ for i in 0 .. s_len - 1 do
+ zeros = 0
+ for j in 0 .. sections - 1 do
+ index = j * s_len + i
+ if line [index, 1] == "0"
+ then zeros += 1
+ end
+ end
+ ones = sections - zeros
+ sum += [zeros, ones] . min
+ end
+ puts sum
+end