aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-251/witawayar/cpp/ch-1.cpp40
-rw-r--r--challenge-251/witawayar/cpp/ch-2.cpp63
-rw-r--r--challenge-251/witawayar/raku/ch-1.raku23
-rw-r--r--challenge-251/witawayar/raku/ch-2.raku34
4 files changed, 160 insertions, 0 deletions
diff --git a/challenge-251/witawayar/cpp/ch-1.cpp b/challenge-251/witawayar/cpp/ch-1.cpp
new file mode 100644
index 0000000000..acc8331116
--- /dev/null
+++ b/challenge-251/witawayar/cpp/ch-1.cpp
@@ -0,0 +1,40 @@
+// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp
+#include <algorithm> // transform
+#include <iostream> // cout
+#include <numeric> // accumulate
+#include <sstream> // stringstream
+#include <vector>
+
+int fun(std::vector<int> const& ints) {
+ std::vector<int> out;
+ std::transform(ints.cbegin(), ints.cend(),
+ ints.crbegin(), std::back_inserter(out),
+ [](const int& a, const int& b) {
+ return std::stoi(std::to_string(a) + std::to_string(b)); });
+ std::size_t out_size = out.size();
+ int sum = std::accumulate(out.cbegin(), out.cbegin() + out_size / 2, 0);
+ if (out_size % 2 != 0)
+ sum += ints[ints.size() / 2];
+
+ return sum;
+}
+
+int main(void) {
+ // Tests
+ std::vector<std::pair<std::vector<int>, int>> tests = {
+ {{6, 12, 25, 1}, 1286},
+ {{10, 7, 31, 5, 2, 2}, 489},
+ {{1, 2, 10}, 112}
+ };
+
+ for (const auto& [input, expected_output] : tests) {
+ int got = fun(input);
+ if (got != expected_output) {
+ std::stringstream error_msg;
+ error_msg << "Expected " << expected_output << ", got " << got;
+ throw std::runtime_error(error_msg.str());
+ }
+ }
+
+ std::cout << "done-testing, success\n";
+}
diff --git a/challenge-251/witawayar/cpp/ch-2.cpp b/challenge-251/witawayar/cpp/ch-2.cpp
new file mode 100644
index 0000000000..39da0ead44
--- /dev/null
+++ b/challenge-251/witawayar/cpp/ch-2.cpp
@@ -0,0 +1,63 @@
+// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp
+#include <algorithm> // transform
+#include <iostream> // cout
+#include <limits> // numeric_limits
+#include <sstream> // stringstream
+#include <vector>
+
+int fun(std::vector<std::vector<int>> const& matrix) {
+ std::size_t n_rows = matrix.size(), n_cols = matrix[0].size();
+ std::vector<int> row_minimals, col_maximals;
+ row_minimals.reserve(n_rows);
+ col_maximals.reserve(n_cols);
+
+ // Row minimals
+ std::transform(matrix.cbegin(), matrix.cend(),
+ std::back_inserter(row_minimals),
+ [] (std::vector<int> const& row) {
+ return *std::min_element(row.cbegin(), row.cend()); });
+
+ // Column maximals
+ for (std::size_t i{0}; i != n_cols; ++i) {
+ int col_maximal{std::numeric_limits<int>::min()};
+ for (auto row: matrix) {
+ if (row[i] > col_maximal) {
+ col_maximal = row[i];
+ }
+ }
+ col_maximals.push_back(col_maximal);
+ }
+
+ // Set intersection...
+ std::sort(row_minimals.begin(), row_minimals.end());
+ std::sort(col_maximals.begin(), col_maximals.end());
+ std::vector<int> intersections;
+ std::set_intersection(row_minimals.cbegin(), row_minimals.cend(),
+ col_maximals.cbegin(), col_maximals.cend(),
+ std::back_inserter(intersections));
+
+ return intersections.empty()
+ ? -1
+ : intersections[0];
+}
+
+int main(void) {
+ // Tests
+ std::vector<std::pair<std::vector<std::vector<int>>, int>> tests = {
+ {{{3, 7, 8}, {9, 11, 13}, {15, 16, 17}}, 15},
+ {{{1, 10, 4, 2}, {9, 3, 8, 7}, {15, 16, 17, 12}}, 12},
+ {{{7, 8}, {1, 2}}, 7},
+ {{{7, 8}, {13, 2}}, -1}
+ };
+
+ for (const auto& [input, expected_output] : tests) {
+ int got = fun(input);
+ if (got != expected_output) {
+ std::stringstream error_msg;
+ error_msg << "Expected " << expected_output << ", got " << got;
+ throw std::runtime_error(error_msg.str());
+ }
+ }
+
+ std::cout << "done-testing, success\n";
+}
diff --git a/challenge-251/witawayar/raku/ch-1.raku b/challenge-251/witawayar/raku/ch-1.raku
new file mode 100644
index 0000000000..751d3ab05d
--- /dev/null
+++ b/challenge-251/witawayar/raku/ch-1.raku
@@ -0,0 +1,23 @@
+my &fun = { (@^ints Z~ @^ints.reverse)[^(* div 2)].sum + (@^ints[* div 2] if @^ints !%% 2) }
+
+#`{
+- Take an array of things via the implicit signature @^ints
+- Zip that array with its reversed version
+ - This essentially takes 1 item from beginning and 1 from end together
+- We don't need the "repeated" zippings, i.e., past the middle point, it will be mirrored
+ - So take until the half of the array via `^(* div 2)` (`*` is passed the array length)
+- We are zipping with `~`, i.e., string gluer, so the numbers will be joined together
+ - Summation of those glueds will be the end result, except...
+- If we had an odd-sized array, the middle guy didn't get accounted for, so we add that
+}
+
+use Test;
+my @tests of Pair =
+ (6, 12, 25, 1) => 1286,
+ (10, 7, 31, 5, 2, 2) => 489,
+ (1, 2, 10) => 112;
+
+for @tests -> (:key($input), :value($expected-output)) {
+ ok fun($input) eqv $expected-output;
+ LAST done-testing;
+}
diff --git a/challenge-251/witawayar/raku/ch-2.raku b/challenge-251/witawayar/raku/ch-2.raku
new file mode 100644
index 0000000000..25e4d456de
--- /dev/null
+++ b/challenge-251/witawayar/raku/ch-2.raku
@@ -0,0 +1,34 @@
+my &fun = { (@^matrix>>.min ∩ ([Z] @^matrix)>>.max).head.key // -1 }
+
+#`{
+- Take single positional Positional argument, @^matrix
+- Take the minimum of each row -- `>>.min` is like `.map(&min)` here
+ - `>>` !== `map` in general though: https://www.reddit.com/r/rakulang/comments/zg4rrq/comment/izgrvt3/?utm_source=share&utm_medium=web2x&context=3
+- Take the maximum of each column
+ - We first "transpose" the ArrayOfArrays with self-zipping
+ - Then take each now-rows' maximal values
+- Intersection of row-minimals and column-maximals is the result
+ - `∩` is the intersection operator (`(&)` in ASCII); operands need not be Setty
+ - The result is a Set -- we need its first (and perhaps the only) element
+ - We reach the first one via `.head`
+ - But since Sets are Maps in disguise with values being `True`, we need a `.key` further
+- If no such intersection exists, we got `Nil`, so `//` (defined-or) will route to -1
+}
+
+use Test;
+my @tests of Pair =
+ [[ 3, 7, 8],
+ [ 9, 11, 13],
+ [15, 16, 17]] => 15,
+ [[ 1, 10, 4, 2],
+ [ 9, 3, 8, 7],
+ [15, 16, 17, 12] ] => 12,
+ [[7, 8],
+ [1, 2]] => 7,
+ [[7, 8],
+ [13, 2]] => -1;
+
+for @tests -> (:key($input), :value($expected-output)) {
+ ok fun($input) eqv $expected-output;
+ LAST done-testing;
+}