aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-077/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-077/arne-sommer/raku/ch-1.p660
-rwxr-xr-xchallenge-077/arne-sommer/raku/ch-2.p643
-rwxr-xr-xchallenge-077/arne-sommer/raku/fibonacci-sum60
-rwxr-xr-xchallenge-077/arne-sommer/raku/fibonacci-sum-first82
-rwxr-xr-xchallenge-077/arne-sommer/raku/fibonacci-sum-first-verbose84
-rwxr-xr-xchallenge-077/arne-sommer/raku/lonely-x43
-rw-r--r--challenge-077/arne-sommer/raku/matrix1.txt3
-rw-r--r--challenge-077/arne-sommer/raku/matrix2.txt4
9 files changed, 380 insertions, 0 deletions
diff --git a/challenge-077/arne-sommer/blog.txt b/challenge-077/arne-sommer/blog.txt
new file mode 100644
index 0000000000..80ea989b42
--- /dev/null
+++ b/challenge-077/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/lonely-sum.html
diff --git a/challenge-077/arne-sommer/raku/ch-1.p6 b/challenge-077/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..719e169ecf
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,60 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose), :u(:$upto));
+
+my $label;
+my $found;
+
+
+$upto
+ ?? (1..$N).map({ fibonal-decomposition($_) })
+ !! fibonal-decomposition($N);
+
+sub fibonal-decomposition($target)
+{
+ $found = False;
+ $label = 'a';
+
+ my @fibs;
+ my $fibonacci := (1, 1, * + * ... Inf);
+
+ for $fibonacci -> $fib { last if $fib > $target; @fibs.unshift: $fib; }
+ @fibs.pop;
+
+ if $verbose
+ {
+ say ": Target: $target" if $upto;
+ say ": Fibonacci (reverse): { @fibs.join(", ") }";
+ }
+
+ recurse(0, (), @fibs, $target);
+
+ say "0" unless $found;
+ say "" if $upto;
+}
+
+sub recurse ($value is copy, @values is copy, @fibonacci is copy, $input)
+{
+ if $value < $input
+ {
+ while @fibonacci
+ {
+ my $add = @fibonacci.shift;
+
+ if $value + $add <= $input
+ {
+ my $value2 = $value + $add;
+ my @values2 = @values.clone.push: $add;
+ recurse($value2, @values2, @fibonacci, $input);
+ }
+ }
+ }
+
+ elsif $value == $input
+ {
+ say "{ $label++ }) { @values.join(' + ') } = $input";
+ $found = True;
+ }
+}
diff --git a/challenge-077/arne-sommer/raku/ch-2.p6 b/challenge-077/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..035cf2d973
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($matrix where $matrix.IO.f && $matrix.IO.r = 'matrix1.txt',
+ :v(:$verbose));
+
+my @matrix = $matrix.IO.lines.map( *.words.list );
+
+die "Uneven grid row length" unless [==] @(@matrix)>>.elems;
+
+die "Single characters only in the grid" if @(@matrix)>>.chars>>.max.max > 1;
+
+my $rows = @matrix.elems;
+my $cols = @matrix[0].elems;
+
+my $is_lonely = 0;
+
+for ^$rows -> $row
+{
+ for ^$cols -> $col
+ {
+ say ": [$row,$col] -> @matrix[$row][$col] { is_lonely(@matrix, $row, $col) ?? 'is lonely' !! '' }" if $verbose;
+ $is_lonely++ if is_lonely(@matrix, $row, $col);
+ }
+}
+
+say $is_lonely;
+
+sub is_lonely (@matrix, $row, $col)
+{
+ return False if @matrix[$row][$col] eq 'O';
+
+ for (-1, 0, 1) -> $r
+ {
+ for (-1, 0, 1) -> $c
+ {
+ next if $r == $c == 0;
+ next unless @matrix[$row + $r][$col + $c].defined;
+
+ return False if @matrix[$row + $r][$col + $c] eq 'X';
+ }
+ }
+ return True;
+}
diff --git a/challenge-077/arne-sommer/raku/fibonacci-sum b/challenge-077/arne-sommer/raku/fibonacci-sum
new file mode 100755
index 0000000000..719e169ecf
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/fibonacci-sum
@@ -0,0 +1,60 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose), :u(:$upto));
+
+my $label;
+my $found;
+
+
+$upto
+ ?? (1..$N).map({ fibonal-decomposition($_) })
+ !! fibonal-decomposition($N);
+
+sub fibonal-decomposition($target)
+{
+ $found = False;
+ $label = 'a';
+
+ my @fibs;
+ my $fibonacci := (1, 1, * + * ... Inf);
+
+ for $fibonacci -> $fib { last if $fib > $target; @fibs.unshift: $fib; }
+ @fibs.pop;
+
+ if $verbose
+ {
+ say ": Target: $target" if $upto;
+ say ": Fibonacci (reverse): { @fibs.join(", ") }";
+ }
+
+ recurse(0, (), @fibs, $target);
+
+ say "0" unless $found;
+ say "" if $upto;
+}
+
+sub recurse ($value is copy, @values is copy, @fibonacci is copy, $input)
+{
+ if $value < $input
+ {
+ while @fibonacci
+ {
+ my $add = @fibonacci.shift;
+
+ if $value + $add <= $input
+ {
+ my $value2 = $value + $add;
+ my @values2 = @values.clone.push: $add;
+ recurse($value2, @values2, @fibonacci, $input);
+ }
+ }
+ }
+
+ elsif $value == $input
+ {
+ say "{ $label++ }) { @values.join(' + ') } = $input";
+ $found = True;
+ }
+}
diff --git a/challenge-077/arne-sommer/raku/fibonacci-sum-first b/challenge-077/arne-sommer/raku/fibonacci-sum-first
new file mode 100755
index 0000000000..47bdb4503c
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/fibonacci-sum-first
@@ -0,0 +1,82 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose), :u(:$upto), :f(:$first));
+
+my $label;
+my $found;
+my $found2 = True;
+
+if $first
+{
+ my $current = 1;
+
+ while ( $found2 )
+ {
+ fibonal-decomposition($current);
+ say ": Checking $current" if $verbose;
+ $current++;
+ }
+}
+else
+{
+ $upto
+ ?? (1..$N).map({ fibonal-decomposition($_) })
+ !! fibonal-decomposition($N);
+}
+
+sub fibonal-decomposition($target)
+{
+ $found = False;
+ $label = 'a';
+
+ my @fibs;
+ my $fibonacci := (1, 1, * + * ... Inf);
+
+ for $fibonacci -> $fib { last if $fib > $target; @fibs.unshift: $fib; }
+ @fibs.pop;
+
+ if $verbose && ! $first
+ {
+ say ": Target: $target" if $upto;
+ say ": Fibonacci (reverse): { @fibs.join(", ") }";
+ }
+
+ recurse(0, (), @fibs, $target);
+
+ unless $first
+ {
+ say "0" unless $found;
+ say "" if $upto;
+ }
+}
+
+
+sub recurse ($value is copy, @values is copy, @fibonacci is copy, $input)
+{
+ if $value < $input
+ {
+ while @fibonacci
+ {
+ my $add = @fibonacci.shift;
+
+ if $value + $add <= $input
+ {
+ my $value2 = $value + $add;
+ my @values2 = @values.clone.push: $add;
+ recurse($value2, @values2, @fibonacci, $input);
+ }
+ }
+ }
+
+ elsif $value == $input
+ {
+ say "{ $label++ }) { @values.join(' + ') } = $input" unless $first;
+ $found = True;
+ }
+ elsif $first
+ {
+ $found2 = False;
+ }
+}
diff --git a/challenge-077/arne-sommer/raku/fibonacci-sum-first-verbose b/challenge-077/arne-sommer/raku/fibonacci-sum-first-verbose
new file mode 100755
index 0000000000..33e8296265
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/fibonacci-sum-first-verbose
@@ -0,0 +1,84 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose), :u(:$upto), :f(:$first));
+
+my $label;
+my $found;
+my $found2 = True;
+my $recursion;
+
+if $first
+{
+ my $current = 1;
+
+ while ( $found2 )
+ {
+ $recursion = 0;
+ fibonal-decomposition($current);
+ say ": Checking $current [Recursive calls: $recursion]" if $verbose;
+ $current++;
+ }
+}
+else
+{
+ $upto
+ ?? (1..$N).map({ fibonal-decomposition($_) })
+ !! fibonal-decomposition($N);
+}
+
+sub fibonal-decomposition($target)
+{
+ $found = False;
+ $label = 'a';
+
+ my @fibs;
+ my $fibonacci := (1, 1, * + * ... Inf);
+
+ for $fibonacci -> $fib { last if $fib > $target; @fibs.unshift: $fib; }
+ @fibs.pop;
+
+ if $verbose && ! $first
+ {
+ say ": Target: $target" if $upto;
+ say ": Fibonacci (reverse): { @fibs.join(", ") }";
+ }
+
+ recurse(0, (), @fibs, $target);
+
+ unless $first
+ {
+ say "0" unless $found;
+ say "" if $upto;
+ }
+}
+
+sub recurse ($value is copy, @values is copy, @fibonacci is copy, $input)
+{
+ $recursion++;
+ if $value < $input
+ {
+ while @fibonacci
+ {
+ my $add = @fibonacci.shift;
+
+ if $value + $add <= $input
+ {
+ my $value2 = $value + $add;
+ my @values2 = @values.clone.push: $add;
+ recurse($value2, @values2, @fibonacci, $input);
+ }
+ }
+ }
+
+ elsif $value == $input
+ {
+ say "{ $label++ }) { @values.join(' + ') } = $input" unless $first;
+ $found = True;
+ }
+ elsif $first
+ {
+ $found2 = False;
+ }
+}
diff --git a/challenge-077/arne-sommer/raku/lonely-x b/challenge-077/arne-sommer/raku/lonely-x
new file mode 100755
index 0000000000..035cf2d973
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/lonely-x
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($matrix where $matrix.IO.f && $matrix.IO.r = 'matrix1.txt',
+ :v(:$verbose));
+
+my @matrix = $matrix.IO.lines.map( *.words.list );
+
+die "Uneven grid row length" unless [==] @(@matrix)>>.elems;
+
+die "Single characters only in the grid" if @(@matrix)>>.chars>>.max.max > 1;
+
+my $rows = @matrix.elems;
+my $cols = @matrix[0].elems;
+
+my $is_lonely = 0;
+
+for ^$rows -> $row
+{
+ for ^$cols -> $col
+ {
+ say ": [$row,$col] -> @matrix[$row][$col] { is_lonely(@matrix, $row, $col) ?? 'is lonely' !! '' }" if $verbose;
+ $is_lonely++ if is_lonely(@matrix, $row, $col);
+ }
+}
+
+say $is_lonely;
+
+sub is_lonely (@matrix, $row, $col)
+{
+ return False if @matrix[$row][$col] eq 'O';
+
+ for (-1, 0, 1) -> $r
+ {
+ for (-1, 0, 1) -> $c
+ {
+ next if $r == $c == 0;
+ next unless @matrix[$row + $r][$col + $c].defined;
+
+ return False if @matrix[$row + $r][$col + $c] eq 'X';
+ }
+ }
+ return True;
+}
diff --git a/challenge-077/arne-sommer/raku/matrix1.txt b/challenge-077/arne-sommer/raku/matrix1.txt
new file mode 100644
index 0000000000..f8beeb613f
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/matrix1.txt
@@ -0,0 +1,3 @@
+O O X
+X O O
+X O O
diff --git a/challenge-077/arne-sommer/raku/matrix2.txt b/challenge-077/arne-sommer/raku/matrix2.txt
new file mode 100644
index 0000000000..d81104f1b2
--- /dev/null
+++ b/challenge-077/arne-sommer/raku/matrix2.txt
@@ -0,0 +1,4 @@
+O O X O
+X O O O
+X O O X
+O X O O