aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/abigail
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-10-30 18:26:37 +0100
committerAbigail <abigail@abigail.be>2020-10-30 18:26:37 +0100
commit2cef3a921301dde0907e738c4722fe9f8271f9ee (patch)
treeda7909d7ca72d32457f3a728423f26ba41cccc92 /challenge-084/abigail
parent809d6c4eb607b1393ec724a4897ca6ea1da4bc9f (diff)
downloadperlweeklychallenge-club-2cef3a921301dde0907e738c4722fe9f8271f9ee.tar.gz
perlweeklychallenge-club-2cef3a921301dde0907e738c4722fe9f8271f9ee.tar.bz2
perlweeklychallenge-club-2cef3a921301dde0907e738c4722fe9f8271f9ee.zip
AWK solution for week 84/part 2
Diffstat (limited to 'challenge-084/abigail')
-rw-r--r--challenge-084/abigail/awk/ch-2.awk46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-084/abigail/awk/ch-2.awk b/challenge-084/abigail/awk/ch-2.awk
new file mode 100644
index 0000000000..ddb4a84b5a
--- /dev/null
+++ b/challenge-084/abigail/awk/ch-2.awk
@@ -0,0 +1,46 @@
+#
+# You are given matrix of size m x n with only 1 and 0.
+#
+# Write a script to find the count of squares having all four corners set as 1.
+#
+
+BEGIN {
+ FS = " "; # Split input on spaces.
+ x = 0; # Array indices start at 1.
+}
+
+#
+# Read the input, store it in array matrix.
+#
+{
+ x ++;
+ for (y = 1; y <= NF; y ++) {
+ matrix [x, y] = +$y;
+ }
+ y = NF;
+}
+
+END {
+ #
+ # Now, x and y are the maximum index values.
+ #
+ count = 0;
+
+ #
+ # Cubic algorithm, counting all possible squares.
+ #
+ for (i = 1; i <= x; i ++) {
+ for (j = 1; j <= y; j ++) {
+ if (matrix [i, j]) {
+ for (k = 1; i + k <= x && j + k <= y; k ++) {
+ if (matrix [i, j + k] &&
+ matrix [i + k, j ] &&
+ matrix [i + k, j + k]) {
+ count ++;
+ }
+ }
+ }
+ }
+ }
+ print count;
+}