diff options
| author | Packy Anderson <PackyAnderson@gmail.com> | 2024-05-06 23:41:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-06 23:41:04 -0400 |
| commit | 8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b (patch) | |
| tree | 4cc9a6443382d2a32208246c481cd566b6ea0142 /challenge-266/2colours/php | |
| parent | e4a2c66b30346606d347ecc4a08b83b733797d2a (diff) | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.gz perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.bz2 perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.zip | |
Merge branch 'manwar:master' into challenge-268
Diffstat (limited to 'challenge-266/2colours/php')
| -rw-r--r-- | challenge-266/2colours/php/ch-1.php | 14 | ||||
| -rw-r--r-- | challenge-266/2colours/php/ch-2.php | 22 |
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-266/2colours/php/ch-1.php b/challenge-266/2colours/php/ch-1.php new file mode 100644 index 0000000000..3079fd5dff --- /dev/null +++ b/challenge-266/2colours/php/ch-1.php @@ -0,0 +1,14 @@ +<?php + +echo '$line1 = '; +$line1 = preg_split('/\s+/', rtrim(fgets(STDIN))); +echo '$line2 = '; +$line2 = preg_split('/\s+/', rtrim(fgets(STDIN))); + +$word_frequencies = array_count_values($line1); +foreach (array_count_values($line2) as $key => $value) { + @$word_frequencies[$key] += $value; +} + +$solution = array_keys(array_filter($word_frequencies, fn($freq) => $freq === 1)) ?: ['']; +var_dump($solution); diff --git a/challenge-266/2colours/php/ch-2.php b/challenge-266/2colours/php/ch-2.php new file mode 100644 index 0000000000..c8f7064f37 --- /dev/null +++ b/challenge-266/2colours/php/ch-2.php @@ -0,0 +1,22 @@ +<?php + +$matrix = json_decode(file_get_contents('php://stdin')); +$size = count($matrix[0]); # let's hope it's actually a square matrix... + +// all 0's except for those 2 - which may be one if it's the middle +function is_row_compliant($row, $diagonal_position, $size) +{ + $is_middle = $diagonal_position === $size - 1 - $diagonal_position; + return @array_count_values($row)[0] === $size - ($is_middle ? 1 : 2) && $row[$diagonal_position] !== 0 && $row[$size - 1 - $diagonal_position] !== 0; +} + +//[$diagonal_position, $solution] = array_reduce($matrix, fn ($state, $row) => [$state[0] + 1, $state[1] && is_row_compliant($row, $state[0], $size)], [0, true]); # is this really worth it? :D +$solution = true; +foreach ($matrix as $row_index => $row) { + if (!is_row_compliant($row, $row_index, $size)) { + $solution = false; + break; + } +} + +var_dump($solution);
\ No newline at end of file |
