diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 18:33:35 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 18:33:35 +0100 |
| commit | c6bb015992c65d4248161ca12c22a1af6dd29711 (patch) | |
| tree | e83b82509ed781db424efbdcd77ac6bc4344a0f1 /challenge-151 | |
| parent | acbd8f6b2a7471edbe77f9ea08ab82db66e1c304 (diff) | |
| download | perlweeklychallenge-club-c6bb015992c65d4248161ca12c22a1af6dd29711.tar.gz perlweeklychallenge-club-c6bb015992c65d4248161ca12c22a1af6dd29711.tar.bz2 perlweeklychallenge-club-c6bb015992c65d4248161ca12c22a1af6dd29711.zip | |
Week 151: Make the algorithm for part 2 even simpler.
No need for an additional array: add two 0's to the array of
valuables, and modify in situ.
Diffstat (limited to 'challenge-151')
| -rw-r--r-- | challenge-151/abigail/awk/ch-2.awk | 7 | ||||
| -rw-r--r-- | challenge-151/abigail/bash/ch-2.sh | 19 | ||||
| -rw-r--r-- | challenge-151/abigail/c/ch-2.c | 29 | ||||
| -rw-r--r-- | challenge-151/abigail/lua/ch-2.lua | 15 | ||||
| -rw-r--r-- | challenge-151/abigail/node/ch-2.js | 14 | ||||
| -rw-r--r-- | challenge-151/abigail/perl/ch-2.pl | 15 | ||||
| -rw-r--r-- | challenge-151/abigail/python/ch-2.py | 11 |
7 files changed, 44 insertions, 66 deletions
diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk index 65428f81e8..732444708f 100644 --- a/challenge-151/abigail/awk/ch-2.awk +++ b/challenge-151/abigail/awk/ch-2.awk @@ -13,12 +13,9 @@ function max (a, b) { } { - delete best - best [NF + 1] = 0 - best [NF + 2] = 0 for (i = NF; i > 2; i --) { - best [i] = max($i + best [i + 2], best [i + 1]) + $i = max($i + $(i + 2), $(i + 1)) } - print $1 + best [3] + print $1 + $3 } diff --git a/challenge-151/abigail/bash/ch-2.sh b/challenge-151/abigail/bash/ch-2.sh index 6a9eceec29..cb009e0e16 100644 --- a/challenge-151/abigail/bash/ch-2.sh +++ b/challenge-151/abigail/bash/ch-2.sh @@ -10,17 +10,14 @@ set -f -declare -a best - -while read -a houses -do size=${#houses[@]} - best[$((size + 0))]=0 - best[$((size + 1))]=0 - for ((i = size - 1; i >= 2; i --)) - do ((val1 = ${houses[$i]} + ${best[$((i + 2))]})) - ((val2 = ${best[$((i + 1))]})) - best[$i]=$((val1 < val2 ? val2 : val1)) +while read -a h +do h[${#h[@]}]=0 + h[${#h[@]}]=0 + for ((i = ${#h[@]} - 3; i >= 2; i --)) + do ((val1 = ${h[$i]} + ${h[$((i + 2))]})) + ((val2 = ${h[$((i + 1))]})) + h[$i]=$((val1 < val2 ? val2 : val1)) done - echo $((${houses[0]} + ${best[2]})) + echo $((${h[0]} + ${h[2]})) done diff --git a/challenge-151/abigail/c/ch-2.c b/challenge-151/abigail/c/ch-2.c index 34e7e24ac1..9c46968f80 100644 --- a/challenge-151/abigail/c/ch-2.c +++ b/challenge-151/abigail/c/ch-2.c @@ -21,39 +21,30 @@ int main (void) { while ((str_len = getline (&line, &len, stdin)) != -1) { char * line_ptr = line; - int * houses = NULL; - int * best = NULL; + int * h = NULL; int val; int offset; - int nr_of_houses = 0; + int sz = 0; while (sscanf (line_ptr, "%d%n", &val, &offset) == 1) { - if ((houses = (int *) - realloc (houses, ++ nr_of_houses * sizeof (int))) == NULL) { + if ((h = (int *) realloc (h, (2 + ++ sz) * sizeof (int))) == NULL) { perror ("Recalloc failed"); exit (1); } - houses [nr_of_houses - 1] = val; + h [sz - 1] = val; line_ptr += offset; } - if ((best = (int *) - malloc ((nr_of_houses + 2) * sizeof (int))) == NULL) { - perror ("Malloc failed"); - exit (1); - } - - best [nr_of_houses + 0] = 0; - best [nr_of_houses + 1] = 0; + h [sz + 0] = 0; + h [sz + 1] = 0; - for (int i = nr_of_houses - 1; i >= 2; i --) { - best [i] = max (houses [i] + best [i + 2], best [i + 1]); + for (int i = sz - 1; i >= 2; i --) { + h [i] = max (h [i] + h [i + 2], h [i + 1]); } - printf ("%d\n", houses [0] + best [2]); + printf ("%d\n", h [0] + h [2]); - free (houses); - free (best); + free (h); } free (line); diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua index 0e644d0a6c..d8ac15c101 100644 --- a/challenge-151/abigail/lua/ch-2.lua +++ b/challenge-151/abigail/lua/ch-2.lua @@ -9,18 +9,17 @@ -- for line in io . lines () do - local houses = {} - local best = {} + local h = {} for val in line : gmatch ("%d+") do - houses [#houses + 1] = val + h [#h + 1] = val end - best [#houses + 1] = 0 - best [#houses + 2] = 0 + h [#h + 1] = 0 + h [#h + 1] = 0 - for i = #houses, 3, -1 do - best [i] = math . max (houses [i] + best [i + 2], best [i + 1]) + for i = #h - 2, 3, -1 do + h [i] = math . max (h [i] + h [i + 2], h [i + 1]) end - print (houses [1] + best [3]) + print (h [1] + h [3]) end diff --git a/challenge-151/abigail/node/ch-2.js b/challenge-151/abigail/node/ch-2.js index 079c61e0ce..11a88c6bc7 100644 --- a/challenge-151/abigail/node/ch-2.js +++ b/challenge-151/abigail/node/ch-2.js @@ -11,13 +11,11 @@ require ('readline') . createInterface ({input: process . stdin}) . on ('line', line => { - let houses = line . trim () . split (/ +/) . map (n => +n) - let best = [] - let size = houses . length - best [size + 0] = 0 - best [size + 1] = 0 - for (let i = size - 1; i >= 2; i --) { - best [i] = Math . max (houses [i] + best [i + 2], best [i + 1]) + let h = line . trim () . split (/ +/) . map (n => +n) + h [h . length] = 0 + h [h . length] = 0 + for (let i = h . length - 3; i >= 2; i --) { + h [i] = Math . max (h [i] + h [i + 2], h [i + 1]) } - console . log (houses [0] + best [2]) + console . log (h [0] + h [2]) }) diff --git a/challenge-151/abigail/perl/ch-2.pl b/challenge-151/abigail/perl/ch-2.pl index 9c1e1d3b60..df731f4cfe 100644 --- a/challenge-151/abigail/perl/ch-2.pl +++ b/challenge-151/abigail/perl/ch-2.pl @@ -34,15 +34,10 @@ use List::Util qw [max]; # And we must always pick the first house. # -sub best ($houses) { - my @best; - $best [$#$houses + 1] = 0; - $best [$#$houses + 2] = 0; - for (my $i = $#$houses; $i >= 2; $i --) { - $best [$i] = max $$houses [$i] + $best [$i + 2], $best [$i + 1]; - } - $$houses [0] + $best [2]; +while (<>) { + my @h = (/[0-9]+/g, 0, 0); + $h [$_] = max $h [$_] + $h [$_ + 2], $h [$_ + 1] for reverse 2 .. $#h - 2; + say $h [0] + $h [2]; } - -say best [/[0-9]+/g] while <>; +__END__ diff --git a/challenge-151/abigail/python/ch-2.py b/challenge-151/abigail/python/ch-2.py index a20a18329b..52522eff0b 100644 --- a/challenge-151/abigail/python/ch-2.py +++ b/challenge-151/abigail/python/ch-2.py @@ -11,8 +11,9 @@ import fileinput for line in fileinput . input (): - houses = list (map (lambda x: int (x), line . strip () . split ())) - best = [0] * (len (houses) + 2) - for i in range (len (houses) - 1, 1, -1): - best [i] = max (houses [i] + best [i + 2], best [i + 1]) - print (houses [0] + best [2]) + h = list (map (lambda x: int (x), line . strip () . split ())) + h . append (0) + h . append (0) + for i in range (len (h) - 3, 1, -1): + h [i] = max (h [i] + h [i + 2], h [i + 1]) + print (h [0] + h [2]) |
