diff options
| author | Abigail <abigail@abigail.be> | 2021-05-26 01:50:36 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-05-26 01:50:36 +0200 |
| commit | 90fbbdb37fe637bca8f39b5008e866db6af94572 (patch) | |
| tree | 1500cce517571cedd0d6eab4958028d0f1bcba02 /challenge-114/abigail | |
| parent | 42fee0cd8e2eb6639761d5bd258267ad75490c16 (diff) | |
| download | perlweeklychallenge-club-90fbbdb37fe637bca8f39b5008e866db6af94572.tar.gz perlweeklychallenge-club-90fbbdb37fe637bca8f39b5008e866db6af94572.tar.bz2 perlweeklychallenge-club-90fbbdb37fe637bca8f39b5008e866db6af94572.zip | |
Bash solution for week 114, part 1
Diffstat (limited to 'challenge-114/abigail')
| -rw-r--r-- | challenge-114/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-114/abigail/bash/ch-1.sh | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-114/abigail/README.md b/challenge-114/abigail/README.md index 399c8978c5..9de879bfeb 100644 --- a/challenge-114/abigail/README.md +++ b/challenge-114/abigail/README.md @@ -17,6 +17,7 @@ Output: 1001 ### Solutions * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-114/abigail/bash/ch-1.sh b/challenge-114/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..25c0a08047 --- /dev/null +++ b/challenge-114/abigail/bash/ch-1.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +# +# Reverse a given string. Put the result into a global variable reverse. +# +function rev() { + local in=$1 + reverse="" + for ((i = 0; i < ${#in}; i ++)) + do reverse=${in:$i:1}$reverse + done +} + +while read number +do if [[ $number =~ ^9+$ ]] + then echo $((number + 2)) + continue + fi + + # + # Split input in parts. Length of part1 and part3 are equal. + # part2 is the middle character if the input number has odd + # length, and empty otherwise. + # + part1=${number:0:$((${#number} / 2))} + part2="" + if ((${#number} % 2)) + then part2=${number:$((${#number} / 2)):1} + fi + part3=${number:$(((${#number} + 1) / 2))} + rev $part1 + if ((${reverse} > ${part3})) + then echo $part1$part2$reverse + continue + fi + + # + # Add one of "$part1$part2", and reverse it. + # + inc=$(($part1$part2 + 1)) + rev $inc + + # + # Print result + # + echo $inc${reverse:$((${#number} % 2))} +done |
