diff options
| author | brtastic <brtastic.dev@gmail.com> | 2021-04-25 01:06:54 +0200 |
|---|---|---|
| committer | brtastic <brtastic.dev@gmail.com> | 2021-04-25 01:06:54 +0200 |
| commit | 1fe3699c0ef3f40a145bb666cbff64a59d3aaba6 (patch) | |
| tree | c214cb300d11bcc6280cafa6b6c93f26f12104e2 | |
| parent | 979274b0b9897a031817d6323f6e177840d475c8 (diff) | |
| download | perlweeklychallenge-club-1fe3699c0ef3f40a145bb666cbff64a59d3aaba6.tar.gz perlweeklychallenge-club-1fe3699c0ef3f40a145bb666cbff64a59d3aaba6.tar.bz2 perlweeklychallenge-club-1fe3699c0ef3f40a145bb666cbff64a59d3aaba6.zip | |
Challenge 109 solutions in PHP (translated from Perl)
| -rw-r--r-- | challenge-109/brtastic/php/ch-1.php | 8 | ||||
| -rw-r--r-- | challenge-109/brtastic/php/ch-2.php | 75 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-109/brtastic/php/ch-1.php b/challenge-109/brtastic/php/ch-1.php new file mode 100644 index 0000000000..cc5c5e6de3 --- /dev/null +++ b/challenge-109/brtastic/php/ch-1.php @@ -0,0 +1,8 @@ +<?php + +foreach(range(1, 20) as $num) { + echo array_sum(array_filter( + $num >= 4 ? range(2, floor($num / 2)) : [], + fn ($divisor) => $num % $divisor == 0 + )) . "\n"; +} 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))); + |
