diff options
| author | Abigail <abigail@abigail.be> | 2021-07-06 20:06:54 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-09 01:35:01 +0200 |
| commit | 2e8a7204a33a3fafb924202ed5abf5985b2db9c4 (patch) | |
| tree | 5fefff070e896c7644a2dae9705961351dead0e4 /challenge-075 | |
| parent | 8261967fc156a8a393ab76068816dba0f4d77182 (diff) | |
| download | perlweeklychallenge-club-2e8a7204a33a3fafb924202ed5abf5985b2db9c4.tar.gz perlweeklychallenge-club-2e8a7204a33a3fafb924202ed5abf5985b2db9c4.tar.bz2 perlweeklychallenge-club-2e8a7204a33a3fafb924202ed5abf5985b2db9c4.zip | |
AWK solutions for week 075
Diffstat (limited to 'challenge-075')
| -rw-r--r-- | challenge-075/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-075/abigail/awk/ch-1.awk | 32 | ||||
| -rw-r--r-- | challenge-075/abigail/awk/ch-2.awk | 54 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-075/abigail/README.md b/challenge-075/abigail/README.md index 419a8a9ec4..6b90bcb443 100644 --- a/challenge-075/abigail/README.md +++ b/challenge-075/abigail/README.md @@ -25,6 +25,7 @@ f) (2, 4) ~~~~ ### Solutions +* [AWK](awk/ch-1.awk) * [Perl](perl/ch-1.pl) @@ -72,4 +73,5 @@ Looking at the above histogram, the largest rectangle `(3 x 5)` is formed by columns `(5, 7 and 5)`. ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) diff --git a/challenge-075/abigail/awk/ch-1.awk b/challenge-075/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..59be0ed92f --- /dev/null +++ b/challenge-075/abigail/awk/ch-1.awk @@ -0,0 +1,32 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +function possibilities (target, from, to, sum, i) { + if (target == 0) {return 1} + if (target < 0 || from > to) {return 0} + + sum = 0 + + for (i = 0; i * coins [from] <= target; i ++) { + sum += possibilities(target - i * coins [from], from + 1, to) + } + + return sum +} + +{ + for (i = 2; i <= NF; i ++) { + coins [i] = $i + } + + print (possibilities($1, 2, NF)) +} + + diff --git a/challenge-075/abigail/awk/ch-2.awk b/challenge-075/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..87458981ce --- /dev/null +++ b/challenge-075/abigail/awk/ch-2.awk @@ -0,0 +1,54 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + + +{ + max_height = 0 + for (i = 1; i <= NF; i ++) { + height [i] = $i + if (height [i] > max_height) { + max_height = height [i] + } + } + + max_area = 0; + + # + # For each height, determine the maximum rectangle + # which fits -- which means we need to maximum amount + # of consequitive columns which are at least that heigh. + # + + for (h = 1; h <= max_height; h ++) { + max = 0; + cur = 0; + for (i = 1; i <= NF; i ++) { + if (height [i] >= h) { + cur ++ + } + else { + if (cur > max) { + max = cur + } + cur = 0 + } + } + if (cur > max) { + max = cur + } + + area = max * h + if (area > max_area) { + max_area = area + } + } + + print (max_area) +} |
