aboutsummaryrefslogtreecommitdiff
path: root/challenge-075
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-07-07 16:16:07 +0200
committerAbigail <abigail@abigail.be>2021-07-09 01:35:20 +0200
commitb2405975ce37d7db5e31deef0df94effa723e4cc (patch)
tree7fee16a87c741739e53e7d3ddf1da082e4cddf0a /challenge-075
parent2e8a7204a33a3fafb924202ed5abf5985b2db9c4 (diff)
downloadperlweeklychallenge-club-b2405975ce37d7db5e31deef0df94effa723e4cc.tar.gz
perlweeklychallenge-club-b2405975ce37d7db5e31deef0df94effa723e4cc.tar.bz2
perlweeklychallenge-club-b2405975ce37d7db5e31deef0df94effa723e4cc.zip
Bash solutions for week 075
Diffstat (limited to 'challenge-075')
-rw-r--r--challenge-075/abigail/README.md2
-rw-r--r--challenge-075/abigail/bash/ch-1.sh43
-rw-r--r--challenge-075/abigail/bash/ch-2.sh45
3 files changed, 90 insertions, 0 deletions
diff --git a/challenge-075/abigail/README.md b/challenge-075/abigail/README.md
index 6b90bcb443..cb325bba30 100644
--- a/challenge-075/abigail/README.md
+++ b/challenge-075/abigail/README.md
@@ -26,6 +26,7 @@ f) (2, 4)
### Solutions
* [AWK](awk/ch-1.awk)
+* [Bash](bash/ch-1.sh)
* [Perl](perl/ch-1.pl)
@@ -74,4 +75,5 @@ is formed by columns `(5, 7 and 5)`.
### Solutions
* [AWK](awk/ch-2.awk)
+* [Bash](bash/ch-2.sh)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-075/abigail/bash/ch-1.sh b/challenge-075/abigail/bash/ch-1.sh
new file mode 100644
index 0000000000..8ece29a51a
--- /dev/null
+++ b/challenge-075/abigail/bash/ch-1.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-1.sh < input-file
+#
+
+set -f
+
+function possibilities () {
+ local target=$1
+ local from=$2
+ local to=$3
+ local i=0
+
+ if ((target == 0))
+ then result=1
+ return
+ fi
+
+ if (((target < 0) || (from > to)))
+ then result=0
+ return
+ fi
+
+ local sum=0
+ for ((i = 0; i * ${coins[from]} <= target; i ++))
+ do possibilities $((target - i * ${coins[from]})) $((from + 1)) $to
+ ((sum += result))
+ done
+
+ ((result = sum))
+ return
+}
+
+while read -a coins
+do target=${coins[0]}
+ possibilities $target 1 $((${#coins[@]} - 1))
+ echo $result
+done
diff --git a/challenge-075/abigail/bash/ch-2.sh b/challenge-075/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..e52994e66e
--- /dev/null
+++ b/challenge-075/abigail/bash/ch-2.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+set -f
+
+while read -a heights
+do ((max_height = 0))
+ for ((i = 0; i < ${#heights[@]}; i ++))
+ do if ((max_height < ${heights[$i]}))
+ then ((max_height = ${heights[$i]}))
+ fi
+ done
+
+ ((max_area = 0))
+ for ((h = 1; h <= max_height; h ++))
+ do ((max = 0))
+ ((cur = 0))
+ for ((i = 0; i < ${#heights[@]}; i ++))
+ do if ((${heights[$i]} >= h))
+ then ((cur ++))
+ else if ((cur > max))
+ then ((max = cur))
+ fi
+ ((cur = 0))
+ fi
+ done
+ if ((cur > max))
+ then ((max = cur))
+ fi
+
+ ((area = max * h))
+ if ((max_area < area))
+ then ((max_area = area))
+ fi
+ done
+
+ echo $max_area
+done