aboutsummaryrefslogtreecommitdiff
path: root/challenge-044/noud
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-01-25 15:03:26 +0100
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-01-25 15:03:26 +0100
commit343ff1c58aa2e5eca8320691dc6c2a0d3e2759ab (patch)
tree4cfc50d5e96d55e7105473470031ffc499a0c24a /challenge-044/noud
parent87a6af9725acf75343b1d590e224d9cfbd5f0cd8 (diff)
downloadperlweeklychallenge-club-343ff1c58aa2e5eca8320691dc6c2a0d3e2759ab.tar.gz
perlweeklychallenge-club-343ff1c58aa2e5eca8320691dc6c2a0d3e2759ab.tar.bz2
perlweeklychallenge-club-343ff1c58aa2e5eca8320691dc6c2a0d3e2759ab.zip
Solution to challenge 44 task 1 in Raku by Noud
Diffstat (limited to 'challenge-044/noud')
-rw-r--r--challenge-044/noud/raku/ch-1.p665
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-044/noud/raku/ch-1.p6 b/challenge-044/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..6d24638566
--- /dev/null
+++ b/challenge-044/noud/raku/ch-1.p6
@@ -0,0 +1,65 @@
+# Only 100, please.
+#
+# You are given a string “123456789”. Write a script that would insert ”+” or
+# ”-” in between digits so that when you evaluate, the result should be 100.
+
+
+# General calculator that handles + and - using Grammars.
+grammar Calculator {
+ rule TOP {
+ <num>+ %% <op>
+ }
+ token op { '+' | '-' };
+ token num { \d+ }
+}
+
+class Arithm {
+ method TOP($/) {
+ my $res = $<num>[0];
+
+ loop (my $i = 1; $i < $<num>.elems; $i++) {
+ my $num = Int($<num>[$i]);
+ given $<op>[$i - 1] {
+ when '+' { $res += $num; }
+ when '-' { $res -= $num; }
+ }
+ }
+
+ $/.make($res);
+ }
+}
+
+# Subroutine helper that creates all combinations using + and - and uses
+# Calculator grammar to check if the combination adds up to the target value.
+sub _pmcomb($action, @rest, $target) {
+ if (@rest.elems == 0) {
+ my $result = Calculator.parse($action, :actions(Arithm)).made;
+ if ($result == $target) {
+ say $action ~ '=' ~ $result;
+ }
+ } elsif ($action.chars == 0) {
+ _pmcomb(Str(@rest.first), @rest.tail(*-1), $target);
+ } else {
+ _pmcomb($action ~ '+' ~ @rest.first, @rest.tail(*-1), $target);
+ _pmcomb($action ~ '-' ~ @rest.first, @rest.tail(*-1), $target);
+ _pmcomb($action ~ '' ~ @rest.first, @rest.tail(*-1), $target);
+ }
+}
+
+sub pmcomb($str, $n) {
+ _pmcomb('', $str.comb, $n);
+}
+
+sub pmcomb100($str) {
+ pmcomb($str, 100);
+}
+
+
+say "All arithmic combinations of 1234567890 that add up to 100:";
+pmcomb100('1234567890');
+
+say "All arithmic combinations of 1234567890 that add up to 101:";
+pmcomb('1234567890', 101);
+
+say "All arithmic combinations of 1133557799 that add up to 42:";
+pmcomb('1133557799', 42);