aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-11-29 01:52:30 +0100
committerAbigail <abigail@abigail.be>2020-11-29 01:52:30 +0100
commit532108d34f2e84608409dc3de72488ba2fe2182b (patch)
tree42f6d739d84864c09e38ed911abde96c3dd28598
parent69edd3eb9851422493ddbd94e9a2b0b2e7071ef1 (diff)
downloadperlweeklychallenge-club-532108d34f2e84608409dc3de72488ba2fe2182b.tar.gz
perlweeklychallenge-club-532108d34f2e84608409dc3de72488ba2fe2182b.tar.bz2
perlweeklychallenge-club-532108d34f2e84608409dc3de72488ba2fe2182b.zip
Node solution for week 88, part 2.
-rw-r--r--challenge-088/abigail/node/ch-2.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/challenge-088/abigail/node/ch-2.js b/challenge-088/abigail/node/ch-2.js
new file mode 100644
index 0000000000..e2a986836a
--- /dev/null
+++ b/challenge-088/abigail/node/ch-2.js
@@ -0,0 +1,92 @@
+//
+// Challenge
+//
+// You are given m x n matrix of positive integers.
+//
+// Write a script to print spiral matrix as list.
+//
+
+//
+// We solve this by keeping track of the boundaries (min_x, min_y, max_x,
+// max_y) of the part of the matrix which still needs to be processed.
+// Initially, min_x and min_y are 0, max_x is the index of the bottom row,
+// and max_y is the index of the right most column.
+//
+// We then process the matrix side by side, first going east (top row),
+// south (left column), west (bottom row), then north (left row). After
+// doing a side, we update the corresponding min/max value. That is,
+// after doing the top row, we increment min_x; right column, decrement
+// max_y; bottom row, decrement max_x; left column, increment min_y.
+//
+// We're done when min_x > max_x, or min_y > max_y.
+//
+
+//
+// Read STDIN. Split on newlines, then on white space.
+//
+let matrix = require ("fs")
+ . readFileSync (0) // Read all.
+ . toString () // Turn it into a string.
+ . split ("\n") // Split on newlines.
+ . filter (_ => _ . length) // Filter empty lines.
+ . map (_ => _ . trim () // Trim white space.
+ . split (/\s+/)) // Split on whitespace.
+;
+
+//
+// Check if all rows are the same length
+//
+matrix . forEach (row => {
+ if (row . length != matrix [0] . length) {
+ throw "Not all rows are of equal length";
+ }
+});
+
+
+let EAST = 0;
+let SOUTH = 1;
+let WEST = 2;
+let NORTH = 3;
+
+let direction = EAST; // Initial direction
+
+let min_x = 0;
+let max_x = matrix . length - 1;
+let min_y = 0;
+let max_y = matrix [0] . length - 1;
+
+
+let output = "";
+while (min_x <= max_x && min_y <= max_y) {
+ if (direction == EAST) {
+ for (let y = min_y; y <= max_y; y ++) {
+ output = output + ", " + matrix [min_x] [y];
+ }
+ min_x ++;
+ }
+ if (direction == SOUTH) {
+ for (let x = min_x; x <= max_x; x ++) {
+ output = output + ", " + matrix [x] [max_y];
+ }
+ max_y --;
+ }
+ if (direction == WEST) {
+ for (let y = max_y; y >= min_y; y --) {
+ output = output + ", " + matrix [max_x] [y];
+ }
+ max_x --;
+ }
+ if (direction == NORTH) {
+ for (let x = max_x; x >= min_x; x --) {
+ output = output + ", " + matrix [x] [min_y];
+ }
+ min_y ++;
+ }
+
+ direction = (direction + 1) % (NORTH + 1);
+}
+
+//
+// Remove leading ", " and print result
+//
+process . stdout . write (output . slice (2) + "\n");