1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
//
// 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";
}
});
//
// Set some variables
//
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;
//
// Spiral down the matrix, putting the results into the string output.
//
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);
}
//
// Print the result, without the leading ", ".
//
process . stdout . write (output . slice (2) + "\n");
|