From cc68e1c2ea0d685072b4063aeb9af50f7e42cd98 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 02:09:46 +0100 Subject: Bash solution for week 097, part 2 --- challenge-097/abigail/bash/ch-1.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-097/abigail/bash/ch-1.sh (limited to 'challenge-097/abigail/bash') diff --git a/challenge-097/abigail/bash/ch-1.sh b/challenge-097/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..e51ebe5562 --- /dev/null +++ b/challenge-097/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh -s SHIFT < input-file +# + +# +# Read the option +# +while getopts "s:" name +do if [ "$name" = "s" ] + then shift=$OPTARG + fi +done + + +# +# Iterate over the input, shifting each line as many times as needed +# +while read line +do for ((i = 0; i < $shift; i ++)) + do line=`echo $line | tr A-Z ZA-Y` + done + echo $line +done -- cgit From c045cac9f990ed22afe8c2630a014c74baea3b46 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 00:29:05 +0100 Subject: Bash solution: disable pathname expansion --- challenge-097/abigail/bash/ch-1.sh | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'challenge-097/abigail/bash') diff --git a/challenge-097/abigail/bash/ch-1.sh b/challenge-097/abigail/bash/ch-1.sh index e51ebe5562..8e275a6519 100644 --- a/challenge-097/abigail/bash/ch-1.sh +++ b/challenge-097/abigail/bash/ch-1.sh @@ -8,6 +8,11 @@ # Run as: bash ch-1.sh -s SHIFT < input-file # +# +# Disable pathname expansion +# +set -f + # # Read the option # -- cgit From f1dbdec48c9b3d003f7f8eaaec4c6070be6652f6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 19:11:09 +0100 Subject: Bash and Ruby solutions for week 97, part 2 --- challenge-097/abigail/bash/ch-2.sh | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-097/abigail/bash/ch-2.sh (limited to 'challenge-097/abigail/bash') 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 -- cgit