aboutsummaryrefslogtreecommitdiff
path: root/challenge-077
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-09-15 19:45:04 +0200
committerAbigail <abigail@abigail.be>2020-09-15 19:45:04 +0200
commita5470d3acea87f11ed71eaf4b61e822664d44008 (patch)
tree4af0113adb73b1cf1056fe7065283dd09f797ea3 /challenge-077
parent21af5125a68724d6a49a4a6522daed767749aab9 (diff)
downloadperlweeklychallenge-club-a5470d3acea87f11ed71eaf4b61e822664d44008.tar.gz
perlweeklychallenge-club-a5470d3acea87f11ed71eaf4b61e822664d44008.tar.bz2
perlweeklychallenge-club-a5470d3acea87f11ed71eaf4b61e822664d44008.zip
Perl and JavaScript solutions for Week 77, Part 2.
Diffstat (limited to 'challenge-077')
-rw-r--r--challenge-077/abigail/Part2/input13
-rw-r--r--challenge-077/abigail/Part2/input24
-rw-r--r--challenge-077/abigail/Part2/output1.exp1
-rw-r--r--challenge-077/abigail/Part2/output2.exp1
-rw-r--r--challenge-077/abigail/Part2/solution.js60
-rw-r--r--challenge-077/abigail/Part2/solution.pl56
6 files changed, 125 insertions, 0 deletions
diff --git a/challenge-077/abigail/Part2/input1 b/challenge-077/abigail/Part2/input1
new file mode 100644
index 0000000000..f8beeb613f
--- /dev/null
+++ b/challenge-077/abigail/Part2/input1
@@ -0,0 +1,3 @@
+O O X
+X O O
+X O O
diff --git a/challenge-077/abigail/Part2/input2 b/challenge-077/abigail/Part2/input2
new file mode 100644
index 0000000000..d81104f1b2
--- /dev/null
+++ b/challenge-077/abigail/Part2/input2
@@ -0,0 +1,4 @@
+O O X O
+X O O O
+X O O X
+O X O O
diff --git a/challenge-077/abigail/Part2/output1.exp b/challenge-077/abigail/Part2/output1.exp
new file mode 100644
index 0000000000..d00491fd7e
--- /dev/null
+++ b/challenge-077/abigail/Part2/output1.exp
@@ -0,0 +1 @@
+1
diff --git a/challenge-077/abigail/Part2/output2.exp b/challenge-077/abigail/Part2/output2.exp
new file mode 100644
index 0000000000..0cfbf08886
--- /dev/null
+++ b/challenge-077/abigail/Part2/output2.exp
@@ -0,0 +1 @@
+2
diff --git a/challenge-077/abigail/Part2/solution.js b/challenge-077/abigail/Part2/solution.js
new file mode 100644
index 0000000000..ee04d5c605
--- /dev/null
+++ b/challenge-077/abigail/Part2/solution.js
@@ -0,0 +1,60 @@
+//
+// Exercise:
+// You are given m x n character matrix consists of O and X only.
+// Write a script to count the total number of X surrounded by O only.
+// Print 0 if none found.
+//
+
+//
+// Read in the board:
+// - Read STDIN
+// - Split by newlines
+// - Split each line on spaces
+// - Map an 'X' to a 1, 'O' to a 0.
+// - Add a 0 to the beginning and end of each line.
+//
+let fs = require ("fs");
+let board = fs . readFileSync (0) . toString () . trim () . split ("\n") .
+ map (line => (line . split (" ")) . map (c => c == "X" ? 1 : 0)) .
+ map (line => ([0] . concat (line) . concat ([0])));
+
+//
+// Add top and bottom with 0s
+//
+board . push (board [0] . map (x => 0));
+board . unshift (board [0] . map (x => 0));
+
+let count = 0;
+
+//
+// Iterate over the cells of the board board, skipping cells on the edge
+// (as we added them). For each 1, check the 8 cells surrounding the cell
+// (this will never be outside of the board). If one of the neighbouring
+// cells is a 1, move on the next cell. If no neighbouring cell is 1,
+// we add 1 to the count.
+//
+for (let x = 1; x < board . length - 1; x ++) {
+ ELEMENT:
+ for (let y = 1; y < board [x] . length - 1; y ++) {
+ if (!board [x] [y]) {
+ continue;
+ }
+ for (let dx = -1; dx <= 1; dx ++) {
+ for (let dy = -1; dy <= 1; dy ++) {
+ if (dx == 0 && dy == 0) {
+ continue;
+ }
+ if (board [x + dx] [y + dy]) {
+ continue ELEMENT;
+ }
+ }
+ }
+ count ++;
+ }
+}
+
+
+//
+// Print the results.
+//
+console . log (count);
diff --git a/challenge-077/abigail/Part2/solution.pl b/challenge-077/abigail/Part2/solution.pl
new file mode 100644
index 0000000000..784f4cd662
--- /dev/null
+++ b/challenge-077/abigail/Part2/solution.pl
@@ -0,0 +1,56 @@
+#!/opt/perl/bin/perl
+
+#
+# Exercise:
+# You are given m x n character matrix consists of O and X only.
+# Write a script to count the total number of X surrounded by O only.
+# Print 0 if none found.
+#
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# Read in the board. Use 1 for an X, and 0 for an 0. Add a band
+# of 0's around the board.
+#
+
+my @board = map {[0, map ({/O/ ? 0 : 1} /[OX]/g), 0]} <>;
+push @board => [(0) x @{$board [0]}];
+unshift @board => [(0) x @{$board [0]}];
+
+my $count = 0;
+
+#
+# Iterate over the cells of the board board, skipping cells on the edge
+# (as we added them). For each 1, check the 8 cells surrounding the cell
+# (this will never be outside of the board). If one of the neighbouring
+# cells is a 1, move on the next cell. If no neighbouring cell is 1,
+# we add 1 to the count.
+#
+for (my $x = 1; $x < @board - 1; $x ++) {
+ ELEMENT:
+ for (my $y = 1; $y < @{$board [$x]} - 1; $y ++) {
+ next unless $board [$x] [$y];
+ foreach my $dx (-1 .. 1) {
+ foreach my $dy (-1 .. 1) {
+ next unless $dx || $dy;
+ next ELEMENT if $board [$x + $dx] [$y + $dy];
+ }
+ }
+ $count ++;
+ }
+}
+
+#
+# Print solution.
+#
+say $count;
+
+__END__