diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:15:20 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:15:20 +0100 |
| commit | 03f28cae3ddea3b08a671dd3f20f3d32777aa4db (patch) | |
| tree | 7aea1f86e706cf8233d89d704ac2342a0c63d059 /challenge-109/brtastic/php/ch-2.php | |
| parent | 46b8aecc9397c6211a1e97a7f0638833726294a2 (diff) | |
| parent | 1ff197d81f941c3dd35d77bec8a0326807e8d2b1 (diff) | |
| download | perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.gz perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.bz2 perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-109/brtastic/php/ch-2.php')
| -rw-r--r-- | challenge-109/brtastic/php/ch-2.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/challenge-109/brtastic/php/ch-2.php b/challenge-109/brtastic/php/ch-2.php new file mode 100644 index 0000000000..304967ffae --- /dev/null +++ b/challenge-109/brtastic/php/ch-2.php @@ -0,0 +1,75 @@ +<?php + +const EL_COUNT = 7; + +function permute($what) +{ + if (count($what) == 1) { + return [$what]; + } + + $options = []; + + foreach($what as $el) { + $seen = 0; + array_push($options, ...array_map( + fn ($arr) => [$el, ...$arr], + permute( + array_filter( + $what, + fn ($subel) + => $subel != $el || $seen++ + ) + ) + )); + } + + return $options; +} + +function four_squares($input) +{ + $results = []; + + if (count($input) != EL_COUNT) { + return $results; + } + + foreach (permute($input) as $case) { + $real_case = [0, ...$case, 0]; + $summed_groups = array_map( + fn ($el) + => array_sum( + array_map( + fn ($subel) + => $real_case[$subel], + range($el, $el + 2)) + ), + array_filter( + array_keys($real_case), + fn ($el) + => $el % 2 == 0 && $el <= count($real_case) - 2 + ) + ); + + $matching_first = array_filter( + $summed_groups, + fn ($el) + => $el == $summed_groups[0] + ); + + if (count($matching_first) == count($summed_groups)) { + $case_results = []; + for ($letter = 'a'; $letter != 'h'; ++$letter) { + $case_results[$letter] = array_shift($case); + } + + $results[] = $case_results; + } + } + + return $results; +} + +var_dump(four_squares(range(1, 7))); + |
