From 7098126ba64d41891f3068153715dfefa164d1d7 Mon Sep 17 00:00:00 2001 From: arnesom Date: Fri, 11 Sep 2020 22:58:21 +0200 Subject: 077 Arne Sommer --- challenge-077/arne-sommer/blog.txt | 1 + challenge-077/arne-sommer/raku/ch-1.p6 | 60 ++++++++++++++++ challenge-077/arne-sommer/raku/ch-2.p6 | 43 +++++++++++ challenge-077/arne-sommer/raku/fibonacci-sum | 60 ++++++++++++++++ challenge-077/arne-sommer/raku/fibonacci-sum-first | 82 +++++++++++++++++++++ .../arne-sommer/raku/fibonacci-sum-first-verbose | 84 ++++++++++++++++++++++ challenge-077/arne-sommer/raku/lonely-x | 43 +++++++++++ challenge-077/arne-sommer/raku/matrix1.txt | 3 + challenge-077/arne-sommer/raku/matrix2.txt | 4 ++ 9 files changed, 380 insertions(+) create mode 100644 challenge-077/arne-sommer/blog.txt create mode 100755 challenge-077/arne-sommer/raku/ch-1.p6 create mode 100755 challenge-077/arne-sommer/raku/ch-2.p6 create mode 100755 challenge-077/arne-sommer/raku/fibonacci-sum create mode 100755 challenge-077/arne-sommer/raku/fibonacci-sum-first create mode 100755 challenge-077/arne-sommer/raku/fibonacci-sum-first-verbose create mode 100755 challenge-077/arne-sommer/raku/lonely-x create mode 100644 challenge-077/arne-sommer/raku/matrix1.txt create mode 100644 challenge-077/arne-sommer/raku/matrix2.txt 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 -- cgit