diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-17 00:33:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-17 00:33:10 +0100 |
| commit | 762eb5d20f41a5e64deb412ab997b91017c24b51 (patch) | |
| tree | e81a2c2079ff1e5e536ffb027caa849061121faf | |
| parent | 77ceeeaeb27e8c29dcb477c79669eae1c84ec517 (diff) | |
| parent | 761625d312e1f723e0961946851c50aac7be611b (diff) | |
| download | perlweeklychallenge-club-762eb5d20f41a5e64deb412ab997b91017c24b51.tar.gz perlweeklychallenge-club-762eb5d20f41a5e64deb412ab997b91017c24b51.tar.bz2 perlweeklychallenge-club-762eb5d20f41a5e64deb412ab997b91017c24b51.zip | |
Merge pull request #4732 from stuart-little/stuart-little_126_node
1st commit on 126_node
| -rwxr-xr-x | challenge-126/stuart-little/node/ch-1.js | 11 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/node/ch-2.js | 22 |
2 files changed, 33 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/node/ch-1.js b/challenge-126/stuart-little/node/ch-1.js new file mode 100755 index 0000000000..c54750afee --- /dev/null +++ b/challenge-126/stuart-little/node/ch-1.js @@ -0,0 +1,11 @@ +#!/usr/bin/env node + +// run <script> <number> + +function no1(nr){ + if (nr.length === 0) { return 0 } + if (nr[0] === '1') { return 9**(nr.length-1) } + return (parseInt(nr[0])-1) * 9**(nr.length-1) + no1(nr.slice(1)) +} + +console.log((process.argv[2].indexOf('1') === -1) ? (no1(process.argv[2])) : (no1(process.argv[2])-1)) diff --git a/challenge-126/stuart-little/node/ch-2.js b/challenge-126/stuart-little/node/ch-2.js new file mode 100755 index 0000000000..1475a66d99 --- /dev/null +++ b/challenge-126/stuart-little/node/ch-2.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +// run <script> + +function nbrs(mat,i,j) { + return [-1,0,1].map(x => [-1,0,1].map(y => [i+x,j+y])).flat().filter(p => p[0] !== i || p[1] !== j).filter(p => 0<=p[0] && p[0] < mat.length).filter(p => 0<=p[1] && p[1] < mat[0].length) +} + +inpt=`x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +`.split(/\n/).map(s => s.split(/\s+/)).filter(x => x[0].length>0) + +for (let i=0; i<inpt.length; i++) { + let str="" + for (let j=0; j<inpt[0].length; j++) { + str += (((inpt[i][j] === 'x') ? ('x') : (nbrs(inpt,i,j).filter(p => inpt[p[0]][p[1]] === 'x').length)) + " ") + } + console.log(str) +} |
