aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/2colours/php/ch-1.php
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-13 12:59:13 +0100
committerGitHub <noreply@github.com>2024-06-13 12:59:13 +0100
commited4f7c1bb8c69872e2fc59ea9d7703bb49b32122 (patch)
tree9d0b592273ef5a6b605a0a7cb88cc490213fba59 /challenge-270/2colours/php/ch-1.php
parentce654cb8a5a89ad19b858b600e63e606aa8db195 (diff)
parent45f9b8108fbe9858054ed3ca884a2b1101f9bd66 (diff)
downloadperlweeklychallenge-club-ed4f7c1bb8c69872e2fc59ea9d7703bb49b32122.tar.gz
perlweeklychallenge-club-ed4f7c1bb8c69872e2fc59ea9d7703bb49b32122.tar.bz2
perlweeklychallenge-club-ed4f7c1bb8c69872e2fc59ea9d7703bb49b32122.zip
Merge pull request #10252 from 2colours/branch-for-challenge-270
Solutions for week #270 in PHP
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