aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/abigail/node
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-10-28 16:56:10 +0100
committerAbigail <abigail@abigail.be>2020-10-28 16:56:10 +0100
commitc3be7c56493e4193863155d53bb410c7af21cb28 (patch)
tree4da46589cbb105810ae2265866bfc19f2f9ce853 /challenge-084/abigail/node
parent381da505f3836ed937135b25ec7e8f79b842a914 (diff)
downloadperlweeklychallenge-club-c3be7c56493e4193863155d53bb410c7af21cb28.tar.gz
perlweeklychallenge-club-c3be7c56493e4193863155d53bb410c7af21cb28.tar.bz2
perlweeklychallenge-club-c3be7c56493e4193863155d53bb410c7af21cb28.zip
JavaScript solution for week 84/part 2.
Diffstat (limited to 'challenge-084/abigail/node')
-rw-r--r--challenge-084/abigail/node/ch-2.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-084/abigail/node/ch-2.js b/challenge-084/abigail/node/ch-2.js
new file mode 100644
index 0000000000..9998cdcf8a
--- /dev/null
+++ b/challenge-084/abigail/node/ch-2.js
@@ -0,0 +1,45 @@
+//
+// Read STDIN. Split on newlines, then on whitespace, and turn the results
+// into numbers. Since the input will be newline terminated, we have an
+// empty line to filter out.
+//
+let matrix = require ("fs")
+ . readFileSync (0) // Read a line.
+ . toString () // Turn it into a string.
+ . split ("\n") // Split on newlines.
+ . filter (_ => _) // Remove empty (trailing) line.
+ . map (_ => _ . split (/\s+/) // Split each line on spaces
+ . map (_ => +_)); // Turn into numbers.
+
+let height = matrix . length;
+let width = matrix [0] . length;
+
+//
+// Use a cubic algorithm to count any squares will all 1s at the corners.
+// For each 0 <= x < height, 0 <= y < width, and k > 0 such that x + k < height
+// and y + k < width, we add a square if all of the following are true:
+// matrix [x] [y]
+// matrix [x + k] [y]
+// matrix [x] [y + k]
+// matrix [x + k] [y + k]
+//
+let count = 0;
+for (let x = 0; x < height; x ++) {
+ for (let y = 0; y < width; y ++) {
+ if (!matrix [x] [y]) {
+ continue;
+ }
+ for (let k = 1; x + k < height && y + k < width; k ++) {
+ if (matrix [x] [y + k] &&
+ matrix [x + k] [y] &&
+ matrix [x + k] [y + k]) {
+ count ++
+ }
+ }
+ }
+}
+
+//
+// Print the result.
+//
+console . log (count);