diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-13 22:34:12 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-13 22:34:12 +0100 |
| commit | 0ddd9b996a3df0487be222ba4cfdcd077cc606bb (patch) | |
| tree | ca40b97f8551cd0f66ba27e808441a558d827711 /challenge-077/colin-crain | |
| parent | 278d78532f23217734d6a2233ca18fded2a63c6e (diff) | |
| download | perlweeklychallenge-club-0ddd9b996a3df0487be222ba4cfdcd077cc606bb.tar.gz perlweeklychallenge-club-0ddd9b996a3df0487be222ba4cfdcd077cc606bb.tar.bz2 perlweeklychallenge-club-0ddd9b996a3df0487be222ba4cfdcd077cc606bb.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-077/colin-crain')
| -rw-r--r-- | challenge-077/colin-crain/perl/ch-1.pl | 66 | ||||
| -rw-r--r-- | challenge-077/colin-crain/perl/ch-2.pl | 74 | ||||
| -rw-r--r-- | challenge-077/colin-crain/raku/ch-1.raku | 62 | ||||
| -rw-r--r-- | challenge-077/colin-crain/raku/ch-2.raku | 70 |
4 files changed, 272 insertions, 0 deletions
diff --git a/challenge-077/colin-crain/perl/ch-1.pl b/challenge-077/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..dd838676d4 --- /dev/null +++ b/challenge-077/colin-crain/perl/ch-1.pl @@ -0,0 +1,66 @@ +#! /opt/local/bin/perl +# +# your_pasta_lies_add_up.pl +# +# TASK #1 › Fibonacci Sum +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# UPDATE: 2020-09-07 09:00:00 +# Write a script to find out all possible combination of Fibonacci Numbers +# required to get $N on addition. +# +# You are NOT allowed to repeat a number. Print 0 if none found. +# +# +# Example 1: +# Input: $N = 6 +# +# Output: 3 as (1 + 2 + 3 = 6) +# +# Example 2: +# Input: $N = 9 +# +# Output: 2 as (1 + 8 = 9) +# +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +use warnings; +use strict; +use feature ":5.26"; + +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(sum); + +## ## ## ## ## MAIN: + +my ($target) = shift @ARGV // 9998; + +## make a fib sequence up to the target +my @fib = (0,1); +while (1) { + push @fib, $fib[-1] + $fib[-2]; + last if $fib[-1] + $fib[-2] > $target; +} +## remove 0, remove extra 1 +splice @fib, 0, 2; + +## work +my @output; +for my $len (1..@fib) { + my $iter = combinations( \@fib, $len ); + while ( my $c = $iter->next ) { + push @output, join " + ", @$c if sum( @$c ) == $target; + } +} + +## out +say "target: $target"; +say "found ", scalar @output, " solutions:\n"; + +say "\t$_" for (sort @output ); diff --git a/challenge-077/colin-crain/perl/ch-2.pl b/challenge-077/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..5f4c581105 --- /dev/null +++ b/challenge-077/colin-crain/perl/ch-2.pl @@ -0,0 +1,74 @@ +#! /opt/local/bin/perl +# +# lonely_ex.pl +# +# TASK #2 › Lonely X +# Submitted by: Mohammad S Anwar +# You are given m x n character matrix consists of O and X only. +# +# Write a script to count the total number of X surrounded by O only. +# Print 0 if none found. +# +# Example 1: +# Input: [ O O X ] +# [ X O O ] +# [ X O O ] +# +# Output: 1 as there is only one X at the first row last column +# surrounded by only O. +# Example 2: +# Input: [ O O X O ] +# [ X O O O ] +# [ X O O X ] +# [ O X O O ] +# +# Output: 2 +# +# a) First X found at Row 1 Col 3. +# +# b) Second X found at Row 3 Col 4. +# +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + +use warnings; +use strict; +use feature ":5.26"; +## ## ## ## ## MAIN: + +## in +my @input = @ARGV; +@input = qw(OOXO XOOO XOOX OXOO) if scalar @input == 0 ; +our $mat = [ map { [ split //, $_ ] } @input ]; + +## work +my @lonely; +for my $y (0..scalar @$mat-1) { + for my $x (0..scalar @{$mat->[0]}-1) { + if ($mat->[$y][$x] eq 'X' and is_lonely($x, $y)) { + push @lonely, [$x, $y]; + } + } +} + +## out +say (join ' ', $_->@*) for $mat->@*; +for ( @lonely ) { + my ($col, $row) = map { ++$_ } @$_; + say "the X at column → $col, row down ↓ $row is lonely"; +} + +## ## ## ## ## SUBS: + +sub is_lonely { + my ($x, $y) = @_; + + for my $offset ([1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]) { + next if ($x + $offset->[0] < 0) || ($y + $offset->[1] < 0); + next if ! defined $mat->[ $y + $offset->[1] ][ $x + $offset->[0] ]; + return 0 if $mat->[ $y + $offset->[1] ][ $x + $offset->[0] ] eq 'X'; + } + return 1; +}
\ No newline at end of file diff --git a/challenge-077/colin-crain/raku/ch-1.raku b/challenge-077/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..d123c60358 --- /dev/null +++ b/challenge-077/colin-crain/raku/ch-1.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env perl6 +# +# +# your_pasta_lies_add_up.raku +# +# TASK #1 › Fibonacci Sum +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# UPDATE: 2020-09-07 09:00:00 +# Write a script to find out all possible combination of Fibonacci Numbers +# required to get $N on addition. +# +# You are NOT allowed to repeat a number. Print 0 if none found. +# +# +# Example 1: +# Input: $N = 6 +# +# Output: 3 as (1 + 2 + 3 = 6) +# +# Example 2: +# Input: $N = 9 +# +# Output: 2 as (1 + 8 = 9) +# +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN (Int $target = 9998) ; + +## make a fib sequence up to the target +my @fib = 0, 1 ; +while @fib.tail(2).sum <= $target { + @fib.push: @fib.tail(2).sum ; +} + +## remove F(0), F(1) -- we choose to exclude these values +@fib .= tail(*-2); + +## race runs the grep on 8 cores in batches of 200, for about a 4x speedup +my @output = @fib.combinations.race:8degree:200batch.grep( *.sum == $target ); + + +say "target: $target"; +say "found ", @output.elems, " solutions:\n"; +(.join: ' + ').say for @output.sort; + + + + + + + + + + + diff --git a/challenge-077/colin-crain/raku/ch-2.raku b/challenge-077/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..ae1b942901 --- /dev/null +++ b/challenge-077/colin-crain/raku/ch-2.raku @@ -0,0 +1,70 @@ +#!/usr/bin/env perl6 +# +# +# lonely_ex.pl +# +# TASK #2 › Lonely X +# Submitted by: Mohammad S Anwar +# You are given m x n character matrix consists of O and X only. +# +# Write a script to count the total number of X surrounded by O only. +# Print 0 if none found. +# +# Example 1: +# Input: [ O O X ] +# [ X O O ] +# [ X O O ] +# +# Output: 1 as there is only one X at the first row last column +# surrounded by only O. +# Example 2: +# Input: [ O O X O ] +# [ X O O O ] +# [ X O O X ] +# [ O X O O ] +# +# Output: 2 +# +# a) First X found at Row 1 Col 3. +# +# b) Second X found at Row 3 Col 4. +# +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN (*@input) ; + +## in +@input = qw<OOXO XOOO XOOX OXOO> if @input ~~ Empty; +our @matrix = @input.map({.comb}); +my @lonely; + +## work +for ^@matrix[0].elems X ^@matrix.elems -> ($x, $y) { + if (@matrix[$y][$x] eq 'X' and is_lonely($x, $y)) { + @lonely.push: ($x, $y); + } +} + +## out +.join(' ').say for @matrix; +say ''; +for @lonely -> @point { + my ($col, $row) = @point.map({$_+1}); + say "the X at column → $col, row down ↓ $row is lonely"; +} + +sub is_lonely { + my ($x, $y) = @_; + + for ((-1,0,1) X (-1,0,1)).grep( * !eqv (0,0)) -> $offset { + next if 0 > any ($x + $offset[0]) | ($y + $offset[1]); + next if ! defined @matrix[ $y + $offset[1] ][ $x + $offset[0] ]; + return 0 if @matrix[ $y + $offset[1] ][ $x + $offset[0] ] eq 'X'; + } + return 1; +} |
