aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/abigail/bash/ch-2.sh
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-02-07 18:40:11 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-02-07 18:40:11 +0100
commitb7cd81ad7d65fa804a146fbedb56dbf76b8d3a95 (patch)
treefb5ef461fbe4f2f7d0852ff50189f137bd224e7d /challenge-151/abigail/bash/ch-2.sh
parent98a74265eb245f20dcc038edfca401e67d6cfd4b (diff)
downloadperlweeklychallenge-club-b7cd81ad7d65fa804a146fbedb56dbf76b8d3a95.tar.gz
perlweeklychallenge-club-b7cd81ad7d65fa804a146fbedb56dbf76b8d3a95.tar.bz2
perlweeklychallenge-club-b7cd81ad7d65fa804a146fbedb56dbf76b8d3a95.zip
Week 151: Bash solutions
Diffstat (limited to 'challenge-151/abigail/bash/ch-2.sh')
-rw-r--r--challenge-151/abigail/bash/ch-2.sh46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-151/abigail/bash/ch-2.sh b/challenge-151/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..0b8f213fca
--- /dev/null
+++ b/challenge-151/abigail/bash/ch-2.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+set -f
+
+declare -a best
+
+while read -a houses
+do if [[ ${#houses[@]} -lt 2 ]]
+ then echo ${houses[0]}
+ continue
+ fi
+
+ size=${#houses[@]}
+ for ((i = size - 1; i >= 0; i --))
+ do ((i1 = i + 1))
+ ((i2 = i + 2))
+ if ((i == size - 1))
+ then best[$i]=${houses[$i]}
+ continue
+ fi
+
+ if ((i == 0))
+ then best[$i]=$((${houses[$i]} + ${best[$i2]}))
+ continue
+ fi
+
+ if ((i == size - 2))
+ then val1=${houses[$i]}
+ val2=${best[$i1]}
+ else val1=$((${houses[$i]} + ${best[$i2]}))
+ val2=${best[$i1]}
+ fi
+
+ best[$i]=$((val1 < val2 ? val2 : val1))
+ done
+
+ echo ${best[0]}
+done