aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-22 20:16:14 +0000
committerGitHub <noreply@github.com>2020-03-22 20:16:14 +0000
commitadc080d47c26396fcb85e1135390ca6d00dfea4f (patch)
treeb07cff97666dda17c6233ee83cb140fbe3131d7a
parent6f2624ddd7cc97b7601dd2fefd91ce4a10c10b33 (diff)
parenta9bf7989349c811ad572b4ca9c31fdab3dab099f (diff)
downloadperlweeklychallenge-club-adc080d47c26396fcb85e1135390ca6d00dfea4f.tar.gz
perlweeklychallenge-club-adc080d47c26396fcb85e1135390ca6d00dfea4f.tar.bz2
perlweeklychallenge-club-adc080d47c26396fcb85e1135390ca6d00dfea4f.zip
Merge pull request #1445 from andemark/branch-for-challenge-052
ch-1.p6 rewrite
-rw-r--r--challenge-052/mark-anderson/raku/ch-1.p686
-rw-r--r--challenge-052/mark-anderson/raku/ch-2.p66
2 files changed, 35 insertions, 57 deletions
diff --git a/challenge-052/mark-anderson/raku/ch-1.p6 b/challenge-052/mark-anderson/raku/ch-1.p6
index 846a6a6301..ae6d601155 100644
--- a/challenge-052/mark-anderson/raku/ch-1.p6
+++ b/challenge-052/mark-anderson/raku/ch-1.p6
@@ -1,62 +1,42 @@
#!/usr/bin/env raku
-# A breadth first search solution at
-# https://www.geeksforgeeks.org/stepping-numbers/
-# The C++ code translated to Raku.
+my @results;
-sub MAIN($arg1, $arg2 where $arg1 >= 100 < $arg2 <= 999) {
- my @results;
+sub MAIN($arg1, $arg2 where $arg1 >= 100 < $arg2 <= 999) {
+ my $start = $arg1.polymod(10,10).Array.pop;
+ my $stop = $arg2.polymod(10,10).Array.pop;
+
+ for $start .. $stop -> $num {
+ step_it($arg1, $arg2, $num);
+ say @results.join("\n") if @results;
+ }
+}
- for (0 .. 9) -> $num {
- bfs($arg1, $arg2, $num, @results);
- }
+sub step_it($arg1, $arg2, $num) {
+ @results = Empty;
+ @results.push($num);
- @results.sort.join("\n").say;
-}
+ while @results {
+ last if @results[0].chars == $arg2.chars;
+ my $num = @results.shift;
+ my $last_digit = $num % 10;
-sub bfs(Int $n, Int $m, Int $num, @results) {
- my @nums;
-
- @nums.push($num);
-
- while (@nums) {
- my $stepNum = @nums.shift;
-
- if ($m >= $stepNum >= $n) {
- @results.push($stepNum);
+ unless $last_digit == 0 {
+ my $new = $num ~ $last_digit - 1;
+ push_it($arg1, $arg2, $new);
}
-
- if ($num == 0 || $stepNum > $m) {
- next;
- }
-
- my $lastDigit = $stepNum % 10;
-
- my $stepNumA = $stepNum * 10 + ($lastDigit - 1);
- my $stepNumB = $stepNum * 10 + ($lastDigit + 1);
-
- if ($lastDigit == 0) {
- @nums.push($stepNumB);
- }
-
- elsif ($lastDigit == 9) {
- @nums.push($stepNumA);
- }
-
- else {
- @nums.push($stepNumA);
- @nums.push($stepNumB);
- }
- }
-}
-# A bonus brute force solution
+ unless $last_digit == 9 {
+ my $new = $num ~ $last_digit + 1;
+ push_it($arg1, $arg2, $new);
+ }
+ }
+}
-#sub MAIN($arg1, $arg2 where $arg1 >= 100 < $arg2 <= 999) {
-# for ($arg1..$arg2) -> $num {
-# my @digits = (+$num).polymod(10,10);
-# if (([-] @digits[0,1]).abs == ([-] @digits[1,2]).abs == 1) {
-# say $num;
-# }
-# }
-#}
+sub push_it($arg1, $arg2, $new) {
+ if $new >= $arg1.substr(0, $new.chars) {
+ if $new <= $arg2.substr(0, $new.chars) {
+ @results.push($new);
+ }
+ }
+}
diff --git a/challenge-052/mark-anderson/raku/ch-2.p6 b/challenge-052/mark-anderson/raku/ch-2.p6
index f6f874087b..e532ccd827 100644
--- a/challenge-052/mark-anderson/raku/ch-2.p6
+++ b/challenge-052/mark-anderson/raku/ch-2.p6
@@ -3,11 +3,9 @@
my $person;
my $computer;
-my @coins = <1 2 5 10 20 50 100 200>;
+my @coins = <1 2 5 10 20 50 100 200>.pick(8);
-@coins .= pick(8);
-
-my @players = 2.rand.Int ?? <Person Computer> !! <Computer Person>;
+my @players = <Person Computer>.pick(2);
say @players[0], " goes first";