aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/abigail/awk
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-02-08 11:51:15 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-02-08 11:51:15 +0100
commit5045026f0fe4bf7ba93d00350f8bb6bbde68f5be (patch)
tree0964d86a154ce7fdd4bcdaccdadd955ddc9e6c99 /challenge-151/abigail/awk
parentdfe3d0c53a5e090bb2784fb9487463656fa72387 (diff)
downloadperlweeklychallenge-club-5045026f0fe4bf7ba93d00350f8bb6bbde68f5be.tar.gz
perlweeklychallenge-club-5045026f0fe4bf7ba93d00350f8bb6bbde68f5be.tar.bz2
perlweeklychallenge-club-5045026f0fe4bf7ba93d00350f8bb6bbde68f5be.zip
Week 151: Simplified algorithm for part 2.
By adding two dummy 0's to the "best" array, and not bothering the calculate the best values for the first two houses, we don't have a case study inside the main loop.
Diffstat (limited to 'challenge-151/abigail/awk')
-rw-r--r--challenge-151/abigail/awk/ch-2.awk12
1 files changed, 5 insertions, 7 deletions
diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk
index 73d9b19812..65428f81e8 100644
--- a/challenge-151/abigail/awk/ch-2.awk
+++ b/challenge-151/abigail/awk/ch-2.awk
@@ -14,13 +14,11 @@ function max (a, b) {
{
delete best
- for (i = NF; i > 0; i --) {
- best [i] = NF <= 2 ? $i \
- : i == NF ? $i \
- : i == 1 ? $i + best [i + 2] \
- : i == NF - 1 ? max($i, best [i + 1]) \
- : max($i + best [i + 2], best [i + 1])
+ best [NF + 1] = 0
+ best [NF + 2] = 0
+ for (i = NF; i > 2; i --) {
+ best [i] = max($i + best [i + 2], best [i + 1])
}
- print best [1]
+ print $1 + best [3]
}