aboutsummaryrefslogtreecommitdiff
path: root/challenge-268
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-07 17:28:02 +0100
committerGitHub <noreply@github.com>2024-05-07 17:28:02 +0100
commitc7c54353677613a8be42b9f2b895bd333252f7b9 (patch)
treeaf72f4c451eac303536b0d32b7d40df21f4ef62f /challenge-268
parent655a40c2a6572a2d88b93a2c450059b75ab57adc (diff)
parent66f3d07bccb32507f9dce910643ae3aaf7ad49e8 (diff)
downloadperlweeklychallenge-club-c7c54353677613a8be42b9f2b895bd333252f7b9.tar.gz
perlweeklychallenge-club-c7c54353677613a8be42b9f2b895bd333252f7b9.tar.bz2
perlweeklychallenge-club-c7c54353677613a8be42b9f2b895bd333252f7b9.zip
Merge pull request #10062 from 2colours/branch-for-challenge-268
Branch for challenge 268
Diffstat (limited to 'challenge-268')
-rw-r--r--challenge-268/2colours/groovy/ch-1.groovy15
-rw-r--r--challenge-268/2colours/groovy/ch-2.groovy10
-rw-r--r--challenge-268/2colours/php/ch-1.php18
-rw-r--r--challenge-268/2colours/php/ch-2.php10
4 files changed, 53 insertions, 0 deletions
diff --git a/challenge-268/2colours/groovy/ch-1.groovy b/challenge-268/2colours/groovy/ch-1.groovy
new file mode 100644
index 0000000000..385e3f776f
--- /dev/null
+++ b/challenge-268/2colours/groovy/ch-1.groovy
@@ -0,0 +1,15 @@
+import groovy.json.JsonSlurper
+
+final REPLACEMENTS = ['()', '[]']
+def jsonSlurper = new JsonSlurper()
+def x = jsonSlurper.parseText(System.console().readLine('@x = ').tr(*REPLACEMENTS))
+def y = jsonSlurper.parseText(System.console().readLine('@y = ').tr(*REPLACEMENTS))
+
+x.sort()
+y.sort()
+
+def expected_difference = y[0] - x[0]
+
+def all_expected = [x, y].transpose().collect{ a, b -> b - a }.every{ it == expected_difference }
+
+println all_expected ? expected_difference : 'N/A'
diff --git a/challenge-268/2colours/groovy/ch-2.groovy b/challenge-268/2colours/groovy/ch-2.groovy
new file mode 100644
index 0000000000..5f679e77ac
--- /dev/null
+++ b/challenge-268/2colours/groovy/ch-2.groovy
@@ -0,0 +1,10 @@
+import groovy.json.JsonSlurper
+import groovy.json.JsonOutput
+
+final REPLACEMENTS = ['()', '[]']
+def jsonSlurper = new JsonSlurper()
+def ints = jsonSlurper.parseText(System.console().readLine('@ints = ').tr(*REPLACEMENTS))
+
+ints.sort()
+
+println JsonOutput.toJson(ints.collate(2).collectMany{ it.reverse() }).tr(*REPLACEMENTS.reverse())
diff --git a/challenge-268/2colours/php/ch-1.php b/challenge-268/2colours/php/ch-1.php
new file mode 100644
index 0000000000..789c278b98
--- /dev/null
+++ b/challenge-268/2colours/php/ch-1.php
@@ -0,0 +1,18 @@
+<?php
+
+
+const PARENS = ['(', ')'];
+const BRACKETS = ['[', ']'];
+echo '@x = ';
+$x = json_decode(str_replace(PARENS, BRACKETS, fgets(STDIN)));
+echo '@y = ';
+$y = json_decode(str_replace(PARENS, BRACKETS, fgets(STDIN)));
+
+sort($x);
+sort($y);
+
+$expected_difference = $y[0] - $x[0];
+
+$all_expected = array_product(array_map(fn($a, $b) => $b - $a === $expected_difference, $x, $y));
+
+echo $all_expected ? $expected_difference : 'N/A';
diff --git a/challenge-268/2colours/php/ch-2.php b/challenge-268/2colours/php/ch-2.php
new file mode 100644
index 0000000000..2526cd6f61
--- /dev/null
+++ b/challenge-268/2colours/php/ch-2.php
@@ -0,0 +1,10 @@
+<?php
+
+const PARENS = ['(', ')'];
+const BRACKETS = ['[', ']'];
+echo '@ints = ';
+$ints = json_decode(str_replace(PARENS, BRACKETS, fgets(STDIN)));
+
+sort($ints);
+
+echo str_replace(BRACKETS, PARENS, json_encode(array_merge(...array_map(array_reverse(...), array_chunk($ints, 2)))));