aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/roger-bell-west/javascript/ch-2.js
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2022-11-20 14:22:01 -0500
committerDave Jacoby <jacoby.david@gmail.com>2022-11-20 14:22:01 -0500
commitdd682dfee966fe63cbfbbbf6a9cb903b1d831416 (patch)
treea71619e10c8dcd29fc13a08beb1325f4a7bc5a84 /challenge-191/roger-bell-west/javascript/ch-2.js
parentd6d01468fd7a5647b9ba96ebf7a0157ff79f3352 (diff)
parentbde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff)
downloadperlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.gz
perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.bz2
perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-191/roger-bell-west/javascript/ch-2.js')
-rwxr-xr-xchallenge-191/roger-bell-west/javascript/ch-2.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-191/roger-bell-west/javascript/ch-2.js b/challenge-191/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..424b4641d5
--- /dev/null
+++ b/challenge-191/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,62 @@
+#! /usr/bin/node
+
+"use strict"
+
+function cutelist(n) {
+ let tab = [[false]];
+ let t = [];
+ for (let x = 1; x <= n; x++) {
+ tab.push(new Array(n+1).fill(false));
+ t.push(x);
+ }
+ for (let x = 1; x <= n; x++) {
+ for (let y = 1; y <= x; y++) {
+ if (x % y != 0 && y % x != 0) {
+ tab[x][y] = true;
+ tab[y][x] = true;
+ }
+ }
+ }
+ let count = 0;
+ let stackl = [[]];
+ let stackc = [t];
+ while (stackl.length != 0) {
+ let l = stackl.pop();
+ let c = stackc.pop();
+ if (c.length == 0 && l.length == n) {
+ count++;
+ } else {
+ let place = l.length + 1;
+ for (let candidate of c) {
+ if (!tab[place][candidate]) {
+ let q = Array.from(l);
+ q.push(candidate);
+ stackl.push(q);
+ stackc.push(Array.from(c.filter(i => i != candidate)));
+ }
+ }
+ }
+ }
+ return count;
+}
+
+if (cutelist(2) == 2) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+
+if (cutelist(10) == 700) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+
+if (cutelist(15) == 24679) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");