aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/abigail/bash
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/bash
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/bash')
-rw-r--r--challenge-097/abigail/bash/ch-2.sh45
1 files changed, 45 insertions, 0 deletions
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