diff options
| author | chirvasitua <chirvasitua@gmail.com> | 2021-07-26 12:32:25 -0400 |
|---|---|---|
| committer | chirvasitua <chirvasitua@gmail.com> | 2021-07-26 12:32:25 -0400 |
| commit | 0324029092373f4cce35fbd28a656218221f970a (patch) | |
| tree | 4302f230176af7fd5820e265c7bea2fa2b6c8228 | |
| parent | 4231c2f762b397e1cacd2cb7e3c2799254fcc1a4 (diff) | |
| download | perlweeklychallenge-club-0324029092373f4cce35fbd28a656218221f970a.tar.gz perlweeklychallenge-club-0324029092373f4cce35fbd28a656218221f970a.tar.bz2 perlweeklychallenge-club-0324029092373f4cce35fbd28a656218221f970a.zip | |
1st commit on 123_node
| -rwxr-xr-x | challenge-123/stuart-little/node/ch-1.js | 26 | ||||
| -rwxr-xr-x | challenge-123/stuart-little/node/ch-2.js | 25 |
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-123/stuart-little/node/ch-1.js b/challenge-123/stuart-little/node/ch-1.js new file mode 100755 index 0000000000..5ada161822 --- /dev/null +++ b/challenge-123/stuart-little/node/ch-1.js @@ -0,0 +1,26 @@ +#!/usr/bin/env node + +// run <script> <number $n> to return the first $n ugly numbers + +let memo = new Set([1,2,3,5]) + +function smth5p(n) { + if (memo.has(n)) {return true} + for (let prm of [2,3,5]) { + if (n%prm===0 && memo.has(n/prm)) { + memo.add(n) + return true + } + } + return false +} + +let count=0 +let nr=0 +while (count < parseInt(process.argv[2])) { + nr++ + if (smth5p(nr)) { + count++ + console.log(nr) + } +} diff --git a/challenge-123/stuart-little/node/ch-2.js b/challenge-123/stuart-little/node/ch-2.js new file mode 100755 index 0000000000..0611520f33 --- /dev/null +++ b/challenge-123/stuart-little/node/ch-2.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +// run <script> <x1 y1 x2 y2 ..> + +function sqDist(coords) { + return (coords[2]-coords[0])**2 + (coords[3]-coords[1])**2 +} + +function sqDistHash(coords) { + let hsh={}; + [0,1,2].forEach( function(i) { + [0,1,2,3].slice(i+1).forEach( function(j) { + let dst = sqDist([2*i,2*i+1,2*j,2*j+1].map(ix => coords[ix])) + hsh[dst] = hsh[dst] ? hsh[dst]+1 : 1 + }) + }) + return hsh +} + +function isSq(coords) { + const freqs = Object.values(sqDistHash(coords)) + return freqs.includes(2) && freqs.includes(4) +} + +console.log(isSq(process.argv.slice(2,10).map(x => parseFloat(x))) ? 1 : 0) |
