aboutsummaryrefslogtreecommitdiff
path: root/challenge-077
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-09-21 15:50:45 +0800
committer冯昶 <seaker@qq.com>2020-09-21 15:50:45 +0800
commitacbc30c04977af09ff3e93e74642e325ea1e7083 (patch)
tree67fecb4fb03ba8beb15f2bcdc95d8ef064ac3508 /challenge-077
parentbca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (diff)
downloadperlweeklychallenge-club-acbc30c04977af09ff3e93e74642e325ea1e7083.tar.gz
perlweeklychallenge-club-acbc30c04977af09ff3e93e74642e325ea1e7083.tar.bz2
perlweeklychallenge-club-acbc30c04977af09ff3e93e74642e325ea1e7083.zip
challeng#079, raku solutions
Diffstat (limited to 'challenge-077')
-rwxr-xr-xchallenge-077/feng-chang/raku/ch-1.raku13
-rwxr-xr-xchallenge-077/feng-chang/raku/ch-2.raku18
-rw-r--r--challenge-077/feng-chang/raku/data-c2-01.txt3
-rw-r--r--challenge-077/feng-chang/raku/data-c2-02.txt4
4 files changed, 24 insertions, 14 deletions
diff --git a/challenge-077/feng-chang/raku/ch-1.raku b/challenge-077/feng-chang/raku/ch-1.raku
index 18676223f9..053b739a43 100755
--- a/challenge-077/feng-chang/raku/ch-1.raku
+++ b/challenge-077/feng-chang/raku/ch-1.raku
@@ -3,26 +3,29 @@
my Bool $found = False;
my $Num;
+# $N: = (intial N) - @num.sum
+# @fibs: fibonacci numbers as candidates
+# @num: solution
sub fib-sum(Int:D $N, @fibs, @num) {
if $N == 0 {
say "{ @num.join(' + ') } = $Num";
$found = True;
return;
}
- return unless @fibs;
- return if $N < @fibs[0];
+ return unless @fibs; # all fibonacci numbers are exhausted
+ return if $N < @fibs[0]; # impossible to find a solution
my @F = @fibs;
my @N = @num;
@N.push(@F.shift);
- fib-sum($N - @fibs[0], @F, @N);
+ fib-sum($N - @fibs[0], @F, @N); # try all possibilities that includes current candidate
- fib-sum($N, @F, @num);
+ fib-sum($N, @F, @num); # try all possibilities that excludes current candidate
}
sub MAIN(Int:D $N) {
my Int @fibs = 1, 2, * + * ...^ * > $N;
- $Num = $N;
+ $Num = $N; # remember the initial N
fib-sum($N, @fibs, Array.new);
0.say unless $found;
diff --git a/challenge-077/feng-chang/raku/ch-2.raku b/challenge-077/feng-chang/raku/ch-2.raku
index b9fdf10332..04c51308ff 100755
--- a/challenge-077/feng-chang/raku/ch-2.raku
+++ b/challenge-077/feng-chang/raku/ch-2.raku
@@ -1,7 +1,8 @@
#!/bin/env raku
+# read input and surround matrix with O's
+# e.g.: ./ch-2.raku < data-c2-01.txt
my @a;
-
for $*IN.lines -> $line {
@a.push($line.comb.Array.unshift('O').push('O'));
}
@@ -11,15 +12,14 @@ my UInt $width = @a[0].elems - 2;
@a.unshift(['O' xx $width + 2]);;
@a.push(['O' xx $width + 2]);;
+# count lonely X's
my UInt $cnt = 0;
-for 1..$rows -> $i {
- for 1..$width -> $j {
- my $junc = all(@a[$i-1;$j-1], @a[$i-1;$j], @a[$i-1;$j+1],
- @a[$i;$j-1], @a[$i;$j+1],
- @a[$i+1;$j-1], @a[$i+1;$j], @a[$i+1;$j+1]
- );
- ++$cnt if @a[$i;$j] eq 'X' and $junc eq 'O';
- }
+for 1..$rows X 1..$width -> ($i, $j) {
+ my $junc = all(@a[$i-1;$j-1], @a[$i-1;$j], @a[$i-1;$j+1],
+ @a[$i;$j-1], @a[$i;$j+1],
+ @a[$i+1;$j-1], @a[$i+1;$j], @a[$i+1;$j+1]
+ );
+ ++$cnt if @a[$i;$j] eq 'X' and $junc eq 'O';
}
say $cnt;
diff --git a/challenge-077/feng-chang/raku/data-c2-01.txt b/challenge-077/feng-chang/raku/data-c2-01.txt
new file mode 100644
index 0000000000..32e25b69b9
--- /dev/null
+++ b/challenge-077/feng-chang/raku/data-c2-01.txt
@@ -0,0 +1,3 @@
+OOX
+XOO
+XOO
diff --git a/challenge-077/feng-chang/raku/data-c2-02.txt b/challenge-077/feng-chang/raku/data-c2-02.txt
new file mode 100644
index 0000000000..600d69c9a9
--- /dev/null
+++ b/challenge-077/feng-chang/raku/data-c2-02.txt
@@ -0,0 +1,4 @@
+OOXO
+XOOO
+XOOX
+OXOO