aboutsummaryrefslogtreecommitdiff
path: root/challenge-114/abigail/bash
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-26 02:35:27 +0200
committerAbigail <abigail@abigail.be>2021-05-26 02:35:27 +0200
commitf4a1bd17596e709207db978686479b06325bc417 (patch)
tree0c51480bc8a46391e7e6714b717ae4761e7d9794 /challenge-114/abigail/bash
parent90fbbdb37fe637bca8f39b5008e866db6af94572 (diff)
downloadperlweeklychallenge-club-f4a1bd17596e709207db978686479b06325bc417.tar.gz
perlweeklychallenge-club-f4a1bd17596e709207db978686479b06325bc417.tar.bz2
perlweeklychallenge-club-f4a1bd17596e709207db978686479b06325bc417.zip
Bash solution for week 114, part 2
Diffstat (limited to 'challenge-114/abigail/bash')
-rw-r--r--challenge-114/abigail/bash/ch-2.sh39
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-114/abigail/bash/ch-2.sh b/challenge-114/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..69980bd2b3
--- /dev/null
+++ b/challenge-114/abigail/bash/ch-2.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+function d2b () {
+ local d=$1
+ b=""
+ while ((d > 0))
+ do b=$((d % 2))$b
+ ((d /= 2))
+ done
+}
+
+function b2d () {
+ local b=$1
+ local p=1
+ d=""
+ while ((${#b} > 0))
+ do if [[ ${b:$((${#b} - 1))} == "1" ]]
+ then ((d += p))
+ fi
+ ((p *= 2))
+ b=${b:0:$((${#b} - 1))}
+ done
+}
+
+
+while read number
+do d2b $number
+ [[ 0$b =~ ^(.*)(01)(1*)(0*)$ ]]
+ b2d ${BASH_REMATCH[1]}10${BASH_REMATCH[4]}${BASH_REMATCH[3]}
+ echo $d
+done