aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/2colours/php/ch-1.php
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-270/2colours/php/ch-1.php')
-rw-r--r--challenge-270/2colours/php/ch-1.php21
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-270/2colours/php/ch-1.php b/challenge-270/2colours/php/ch-1.php
new file mode 100644
index 0000000000..a648391f0a
--- /dev/null
+++ b/challenge-270/2colours/php/ch-1.php
@@ -0,0 +1,21 @@
+<?php
+
+$matrix = json_decode(file_get_contents('php://stdin'));
+$solutions = $matrix;
+
+foreach ($matrix as $row => $content) {
+ $valid_row = array_sum($content) === 1;
+ foreach ($solutions[$row] as &$value) {
+ $value *= $valid_row;
+ }
+}
+foreach (array_map(null, ...$matrix) as $column => $content) {
+ $valid_column = array_sum($content) === 1;
+ foreach (array_keys($solutions) as $row) {
+ $solutions[$row][$column] *= $valid_column;
+ }
+}
+
+$result = 0;
+array_walk_recursive($solutions, function ($elem) use (&$result) { $result += $elem; });
+echo $result; \ No newline at end of file