aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--challenge-097/abigail/README.md2
-rw-r--r--challenge-097/abigail/bash/ch-2.sh45
-rw-r--r--challenge-097/abigail/ruby/ch-2.rb47
3 files changed, 94 insertions, 0 deletions
diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md
index a30913db45..3427067f32 100644
--- a/challenge-097/abigail/README.md
+++ b/challenge-097/abigail/README.md
@@ -87,10 +87,12 @@ of a section.
### Solutions
* [AWK](awk/ch-2.awk)
+* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
* [Lua](lua/ch-2.lua)
* [Node](node/ch-2.js)
* [Perl](perl/ch-2.pl)
* [Python](python/ch-2.py)
+* [Ruby](ruby/ch-2.rb)
### Blog
diff --git a/challenge-097/abigail/bash/ch-2.sh b/challenge-097/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..ad82b5ecab
--- /dev/null
+++ b/challenge-097/abigail/bash/ch-2.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh -s SECTIONS < input-file
+#
+
+#
+# Disable pathname expansion
+#
+set -f
+
+#
+# Read the option
+#
+while getopts "s:" name
+do if [ "$name" = "s" ]
+ then sections=$OPTARG
+ fi
+done
+
+
+#
+# Iterate over the input. For each position, count the number of 0s,
+# and calculate the number of 1s. Sum the minimum of those numbers.
+#
+while read line
+do s_len=$((${#line} / $sections))
+ sum=0
+ for ((i = 0; i < s_len; i ++))
+ do zeros=0
+ for ((j = 0; j < sections; j ++))
+ do index=$(($j * $s_len + $i))
+ if [ "${line:$index:1}" == "0" ]
+ then zeros=$(($zeros + 1))
+ fi
+ done
+ ones=$(($sections - $zeros))
+ sum=$(($sum + ($zeros < $ones ? $zeros : $ones) ))
+ done
+ echo $sum
+done
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