diff options
| author | Abigail <abigail@abigail.be> | 2021-07-08 20:39:53 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-09 01:35:21 +0200 |
| commit | 22c000b4f7870e2ee0cb1dbfd349dbcfc5ac4752 (patch) | |
| tree | 3448d88bf75552676518a22c538aca16a9248e8c /challenge-075 | |
| parent | 1f9fd3507cfe52a7e394838835f2dd9958100db0 (diff) | |
| download | perlweeklychallenge-club-22c000b4f7870e2ee0cb1dbfd349dbcfc5ac4752.tar.gz perlweeklychallenge-club-22c000b4f7870e2ee0cb1dbfd349dbcfc5ac4752.tar.bz2 perlweeklychallenge-club-22c000b4f7870e2ee0cb1dbfd349dbcfc5ac4752.zip | |
Node.js solutions for week 075
Diffstat (limited to 'challenge-075')
| -rw-r--r-- | challenge-075/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-075/abigail/node/ch-1.js | 33 | ||||
| -rw-r--r-- | challenge-075/abigail/node/ch-2.js | 43 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-075/abigail/README.md b/challenge-075/abigail/README.md index 4f0ebec2ab..b6143ed93e 100644 --- a/challenge-075/abigail/README.md +++ b/challenge-075/abigail/README.md @@ -29,6 +29,7 @@ f) (2, 4) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) @@ -80,4 +81,5 @@ is formed by columns `(5, 7 and 5)`. * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-075/abigail/node/ch-1.js b/challenge-075/abigail/node/ch-1.js new file mode 100644 index 0000000000..99ea6b661e --- /dev/null +++ b/challenge-075/abigail/node/ch-1.js @@ -0,0 +1,33 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +function possibilities (target, coins, from, to) { + if (target == 0) { + return (1) + } + if (target < 0 || from > to) { + return (0) + } + + let sum = 0 + let i + for (i = 0; i * coins [from] <= target; i ++) { + sum += possibilities (target - i * coins [from], coins, from + 1, to) + } + + return sum +} + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let coins = line . split (' ') . map (_ => +_) + console . log (possibilities (coins [0], coins, 1, coins . length - 1)) +}) diff --git a/challenge-075/abigail/node/ch-2.js b/challenge-075/abigail/node/ch-2.js new file mode 100644 index 0000000000..64b04898ad --- /dev/null +++ b/challenge-075/abigail/node/ch-2.js @@ -0,0 +1,43 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let heights = line . split (" ") . map (_ => +_) + let max_height = Math . max (... heights) + + let max_area = 0 + let i, h + for (h = 1; h <= max_height; h ++) { + let cur = 0 + let max = 0 + for (i = 0; i < heights . length; i ++) { + if (heights [i] >= h) { + cur ++ + } + else { + if (max < cur) { + max = cur + } + cur = 0 + } + } + if (max < cur) { + max = cur + } + + let area = max * h + if (max_area < area) { + max_area = area + } + } + console . log (max_area) +}) |
