From c976e26a845911cf0226844acfc1e4c0e497c8c1 Mon Sep 17 00:00:00 2001 From: Scimon Date: Tue, 10 Aug 2021 17:16:20 +0100 Subject: Updatred based on changed definition --- challenge-125/simon-proctor/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-125/simon-proctor/raku/ch-2.raku b/challenge-125/simon-proctor/raku/ch-2.raku index 2ab4173e5a..45a70774c7 100644 --- a/challenge-125/simon-proctor/raku/ch-2.raku +++ b/challenge-125/simon-proctor/raku/ch-2.raku @@ -146,10 +146,10 @@ multi sub MAIN( *@data ) { say $tree; my $long = shift @routes; - my $result = $long.elems; + my $result = $long.elems - 1; for @routes -> $pos { if ($long (&) $pos).elems == 1 { - $result = $long.elems + $pos.elems - 1; + $result = $long.elems + $pos.elems - 2; last; } } -- cgit From f8c1bed0ca63ca4524d73f3264c1b9b1228609f3 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 16 Aug 2021 09:17:55 +0100 Subject: Challenge 1 --- challenge-126/simon-proctor/raku/ch-1.raku | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 challenge-126/simon-proctor/raku/ch-1.raku diff --git a/challenge-126/simon-proctor/raku/ch-1.raku b/challenge-126/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..479ed8e30d --- /dev/null +++ b/challenge-126/simon-proctor/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku + +#| Given a number N give the count of all numbers between 1 and N inclusive that don't have a 1 in them +sub MAIN ( UInt \N ) { + (1^..N).grep( { ! m/1/ } ).elems.say; +} -- cgit From 0ed415881924a206d814362616c85ccca0f9c0ad Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Aug 2021 10:52:28 +0200 Subject: First task done --- challenge-126/luca-ferrari/raku/ch-1.p6 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 challenge-126/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-126/luca-ferrari/raku/ch-1.p6 b/challenge-126/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..b798fe26f9 --- /dev/null +++ b/challenge-126/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!raku + + +sub MAIN( Int $N ) { + ( 1 .. $N ).grep( * !~~ / 1 / ).elems.say; +} -- cgit From c66717856d3b135efea8c4fadb9a6fbe07020217 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Aug 2021 11:24:38 +0200 Subject: Task 2 done --- challenge-126/luca-ferrari/raku/ch-2.p6 | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 challenge-126/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-126/luca-ferrari/raku/ch-2.p6 b/challenge-126/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..a8499be9b6 --- /dev/null +++ b/challenge-126/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,65 @@ +#!raku + +sub is-mine( @input, $row, $column ) { + return @input[ $row ][ $column ] ~~ 'x'; +} + + +sub compute-mines( @input, $row, $column ) { + my $counter = 0; + + # see where we can move + my ( $can-left, $can-right, $can-up, $can-down ) = + $column > 0, $column < @input[ 0 ].elems, $row > 0, $row < @input.elems; + + + # left, if possible + $counter++ if ( $can-left && is-mine( @input, $row, $column - 1 ) ); + # up if possible + $counter++ if ( $can-up && is-mine( @input, $row - 1, $column ) ); + # down if possible + $counter++ if ( $can-down && is-mine( @input, $row + 1 , $column ) ); + # right if possible + $counter++ if ( $can-right && is-mine( @input, $row, $column + 1 ) ); + + + # left up + $counter++ if ( $can-left && $can-up && is-mine( @input, $row - 1, $column - 1 ) ); + # right up + $counter++ if ( $can-up && $can-right && is-mine( @input, $row - 1, $column + 1 ) ); + # left down + $counter++ if ( $can-left && $can-down && is-mine( @input, $row + 1, $column - 1 ) ); + # right down + $counter++ if ( $can-down && $can-right && is-mine( @input, $row + 1, $column + 1 ) ); + + # left up if possible + + + return $counter; +} + + +sub MAIN() { + my @input = + qw/ x * * * x * x x x x /, + qw/ * * * * * * * * * x /, + qw/ * * * * x * x * x * /, + qw/ * * * x x * * * * * /, + qw/ x * * * x * * * * x /; + + my $rows = @input.elems; + my $columns = @input[ 0 ].elems; + + + + + my ( $current-row, $current-column ) = 0, 0; + for 0 ..^ $rows -> $current-row { + for 0 ..^ $columns -> $current-column { + print is-mine( @input, $current-row, $current-column ) ?? 'x' + !! compute-mines( @input, $current-row, $current-column ); + } + + print "\n"; + } +} -- cgit From 668f75f43892523ef601b7f2b64c413fc4fa2621 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Aug 2021 11:38:44 +0200 Subject: Blog references --- challenge-126/luca-ferrari/blog-1.txt | 1 + challenge-126/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-126/luca-ferrari/blog-1.txt create mode 100644 challenge-126/luca-ferrari/blog-2.txt diff --git a/challenge-126/luca-ferrari/blog-1.txt b/challenge-126/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..8e154cb4bf --- /dev/null +++ b/challenge-126/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/16/PerlWeeklyChallenge126.html#task1 diff --git a/challenge-126/luca-ferrari/blog-2.txt b/challenge-126/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..2234270632 --- /dev/null +++ b/challenge-126/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/16/PerlWeeklyChallenge126.html#task2 -- cgit From 7837479ff1ae88781f1a2645900d6daf1d54cdc6 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 16 Aug 2021 10:55:39 +0100 Subject: first submit - working on better solution for ch-1.pl - there is some pattern in 9^n coming here which I need to get my head round 10^n has 9^n-1 numbers, 2.10^n had 2.9^n numbers, 9.10^n has 8.9^n numbers etc - so should be an easy way to get arbitrary large numbers without a scan! --- challenge-126/james-smith/perl/ch-1.pl | 37 ++++++++++++++++++++++++++++++++++ challenge-126/james-smith/perl/ch-2.pl | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 challenge-126/james-smith/perl/ch-1.pl create mode 100644 challenge-126/james-smith/perl/ch-2.pl diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..ac845eee9a --- /dev/null +++ b/challenge-126/james-smith/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 15, 8 ], + [ 25, 13 ], + [ 100, 80 ], + [ 1000, 728 ], + [ 1100, 728 ], + [ 1200, 728 ], + [ 1300, 728 ], + [ 2000, 729 ], + [ 2100, 809 ], + [ 2200, 810 ], + [ 3000, 1458 ], + [ 4000, 2187 ], + [ 5000, 2916 ], + [ 10000, 6560 ], + [ 100000, 59048 ], + [ 1000000, 531440 ], +); + +is( get_no_one_count($_->[0]), $_->[1] ) foreach @TESTS; +done_testing(); + +sub get_no_one_count { + my $n = shift; + return scalar grep { ! m{1} } 2..$n; +} + diff --git a/challenge-126/james-smith/perl/ch-2.pl b/challenge-126/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..72d66eaf61 --- /dev/null +++ b/challenge-126/james-smith/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @grid = qw(x***x*xxxx *********x ****x*x*x* ***xx***** x***x****x); + +solve(@grid); + +sub solve { + my @res = (''); + ## Map input of strings into an array of 1s (bombs) + 0s (spaces) + my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_; + ## Get max x value.... + my($h,$w) = ($#_,-1 + length $_[0]); + foreach my $y (0..$h) { + $res[-1] .= $in[$y][$_] ? 'x' : + ( $y ? ( ($_?$in[$y-1][$_-1]:0) + +$in[$y-1][$_] + +($_<$w?$in[$y-1][$_+1]:0) ) : 0 ) + + ($_?$in[$y][$_-1]:0) + +$in[$y][$_] + +($_<$w?$in[$y][$_+1]:0) + + ( $y<$h ? ( ($_?$in[$y+1][$_-1]:0) + +$in[$y+1][$_] + +($_<$w?$in[$y+1][$_+1]:0) ) : 0 ) foreach 0..$w; + push @res, ''; + } + print join "\n", @res; +} + -- cgit From 588be19d6c9893bd6fa8e63e44f72147141e7705 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 16 Aug 2021 11:08:00 +0100 Subject: tidied up layout to make readable - even if long lines --- challenge-126/james-smith/perl/ch-2.pl | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/challenge-126/james-smith/perl/ch-2.pl b/challenge-126/james-smith/perl/ch-2.pl index 72d66eaf61..0b0900662c 100644 --- a/challenge-126/james-smith/perl/ch-2.pl +++ b/challenge-126/james-smith/perl/ch-2.pl @@ -10,27 +10,25 @@ use Data::Dumper qw(Dumper); my @grid = qw(x***x*xxxx *********x ****x*x*x* ***xx***** x***x****x); -solve(@grid); +say join "\n",'',@grid,''; +print solve(@grid); +say ''; sub solve { my @res = (''); ## Map input of strings into an array of 1s (bombs) + 0s (spaces) my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_; - ## Get max x value.... - my($h,$w) = ($#_,-1 + length $_[0]); - foreach my $y (0..$h) { + + ## Get index of last row or column... + my( $h, $w ) = ( $#_, -1 + length $_[0] ); + + foreach my $y ( 0 .. $h ) { $res[-1] .= $in[$y][$_] ? 'x' : - ( $y ? ( ($_?$in[$y-1][$_-1]:0) - +$in[$y-1][$_] - +($_<$w?$in[$y-1][$_+1]:0) ) : 0 ) + - ($_?$in[$y][$_-1]:0) - +$in[$y][$_] - +($_<$w?$in[$y][$_+1]:0) + - ( $y<$h ? ( ($_?$in[$y+1][$_-1]:0) - +$in[$y+1][$_] - +($_<$w?$in[$y+1][$_+1]:0) ) : 0 ) foreach 0..$w; + ( $y ? ( $_ ? $in[$y-1][$_-1] : 0 ) + $in[$y-1][$_] + ( $_ < $w ? $in[$y-1][$_+1] : 0 ) : 0 ) + + ( $_ ? $in[$y ][$_-1] : 0 ) + $in[$y ][$_] + ( $_ < $w ? $in[$y ][$_+1] : 0 ) + + ( $y < $h ? ( $_ ? $in[$y+1][$_-1] : 0 ) + $in[$y+1][$_] + ( $_ < $w ? $in[$y+1][$_+1] : 0 ) : 0 ) foreach 0 .. $w; push @res, ''; } - print join "\n", @res; + return join "\n", @res; } -- cgit From c6b2a4e90769b80b8099efdccde08151e388359f Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 16 Aug 2021 12:31:23 +0100 Subject: added a non-scan version --- challenge-126/james-smith/perl/ch-1.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl index ac845eee9a..e680e413c2 100644 --- a/challenge-126/james-smith/perl/ch-1.pl +++ b/challenge-126/james-smith/perl/ch-1.pl @@ -23,10 +23,18 @@ my @TESTS = ( [ 4000, 2187 ], [ 5000, 2916 ], [ 10000, 6560 ], + [ 20000, 6561 ], + [ 25000, 6561+2916 ], + [ 25500, 6561+2916+324 ], + [ 25540, 6561+2916+324+27 ], + [ 25543, 6561+2916+324+27+2 ], [ 100000, 59048 ], [ 1000000, 531440 ], ); +# warn "@{$_} -> ", get_no_one_count_x($_->[0]), "\n" foreach @TESTS; + +is( get_no_one_count_x($_->[0]), $_->[1] ) foreach @TESTS; is( get_no_one_count($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); @@ -35,3 +43,18 @@ sub get_no_one_count { return scalar grep { ! m{1} } 2..$n; } +## Optimized version.... seems to work ... and far better than scan... +sub get_no_one_count_x { + my $n = shift; + my $c = 0; + my $m = 1; + while($n) { + my $t=$n%10; + $c = 0 if $t==1; + $c += $t ? ( $t < 2 ? ($m-1) : ($t-1)*$m ) : 0; + $m *= 9; + $n= int($n/10); + } + return $c; +} + -- cgit From baa2c51105dd5d038a7f6ec0fdb4d721c97b7dc1 Mon Sep 17 00:00:00 2001 From: Mark A Date: Mon, 16 Aug 2021 05:35:20 -0600 Subject: Challenge 126 (Raku) --- challenge-126/mark-anderson/raku/ch-1.raku | 12 +++++++++ challenge-126/mark-anderson/raku/ch-2.raku | 41 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-126/mark-anderson/raku/ch-1.raku create mode 100644 challenge-126/mark-anderson/raku/ch-2.raku diff --git a/challenge-126/mark-anderson/raku/ch-1.raku b/challenge-126/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..277b3fbc11 --- /dev/null +++ b/challenge-126/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku + +use Test; +plan 2; + +is count-numbers(15), 8; +is count-numbers(25), 13; + +sub count-numbers($N) +{ + + grep { not .contains: 1 }, 1..$N; +} diff --git a/challenge-126/mark-anderson/raku/ch-2.raku b/challenge-126/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..75b2856b4c --- /dev/null +++ b/challenge-126/mark-anderson/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +#| rows, columns, percent of mines +sub MAIN($r, $c, $p) +{ + my $n = $r * $c; + my $mines = ($p / 100 * $n).floor; + my @m = ('*' x $n - $mines ~ 'x' x $mines).comb + .pick($n) + .rotor($c) + .map(*.Array); + + .say for @m; + say ' '; + .say for minesweeper(@m); +} + +sub minesweeper(@m) +{ + my $rows = + @m; + my $cols = + @m.head; + + for ^$rows X ^$cols -> ($r, $c) + { + next if @m[$r;$c] eq 'x'; + + @m[$r;$c] = + .comb('x') given gather + { + take @m[$r-1;$c] // '*'; # N + take @m[$r-1;$c+1] // '*'; # NE + take @m[$r;$c+1] // '*'; # E + take @m[$r+1;$c+1] // '*'; # SE + take @m[$r+1;$c] // '*'; # S + take @m[$r+1;$c-1] // '*'; # SW + take @m[$r;$c-1] // '*'; # W + take @m[$r-1;$c-1] // '*'; # NW + } + } + + @m +} -- cgit From 57d72cef9fa6fb0795c4cd315e61a73502d89bb7 Mon Sep 17 00:00:00 2001 From: Mark A Date: Mon, 16 Aug 2021 05:52:13 -0600 Subject: Challenge 126 (Raku) --- challenge-126/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-126/mark-anderson/raku/ch-1.raku b/challenge-126/mark-anderson/raku/ch-1.raku index 277b3fbc11..c88fb4cf5b 100644 --- a/challenge-126/mark-anderson/raku/ch-1.raku +++ b/challenge-126/mark-anderson/raku/ch-1.raku @@ -8,5 +8,5 @@ is count-numbers(25), 13; sub count-numbers($N) { - + grep { not .contains: 1 }, 1..$N; + + grep { not .contains: 1 }, 2..$N; } -- cgit From 39a387fda0327a93d52672621fe7758742fac408 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 16 Aug 2021 13:18:14 +0100 Subject: Solutions for challenge #126 --- challenge-126/roger-bell-west/perl/ch-1.pl | 34 +++++++++++++++++ challenge-126/roger-bell-west/perl/ch-2.pl | 47 +++++++++++++++++++++++ challenge-126/roger-bell-west/python/ch-1.py | 36 ++++++++++++++++++ challenge-126/roger-bell-west/python/ch-2.py | 35 +++++++++++++++++ challenge-126/roger-bell-west/raku/ch-1.p6 | 32 ++++++++++++++++ challenge-126/roger-bell-west/raku/ch-2.p6 | 43 +++++++++++++++++++++ challenge-126/roger-bell-west/ruby/ch-1.rb | 45 ++++++++++++++++++++++ challenge-126/roger-bell-west/ruby/ch-2.rb | 40 ++++++++++++++++++++ challenge-126/roger-bell-west/rust/ch-1.rs | 44 ++++++++++++++++++++++ challenge-126/roger-bell-west/rust/ch-2.rs | 56 ++++++++++++++++++++++++++++ 10 files changed, 412 insertions(+) create mode 100755 challenge-126/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-126/roger-bell-west/perl/ch-2.pl create mode 100755 challenge-126/roger-bell-west/python/ch-1.py create mode 100755 challenge-126/roger-bell-west/python/ch-2.py create mode 100755 challenge-126/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-126/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-126/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-126/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-126/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-126/roger-bell-west/rust/ch-2.rs diff --git a/challenge-126/roger-bell-west/perl/ch-1.pl b/challenge-126/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..fbab7a3346 --- /dev/null +++ b/challenge-126/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,34 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 4; + +is(cn(15),8,'example 1'); +is(cn(25),13,'example 2'); +is(cn(10000),6560,'example 3'); +is(cn(100000000),43046720,'example 4'); + +sub cn { + my $n=shift; + my $k=$n; + my @digits; + while ($k>0) { + my $d=$k%10; + if ($d==1) { + @digits=(8) x scalar(@digits); + } + if ($d>0) { + $d--; + } + push @digits,$d; + $k=int($k/10); + } + my $tc=0; + foreach my $i (reverse(0..$#digits)) { + $tc*=9; + $tc+=$digits[$i]; + } + return $tc; +} diff --git a/challenge-126/roger-bell-west/perl/ch-2.pl b/challenge-126/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..146cd920a0 --- /dev/null +++ b/challenge-126/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,47 @@ +#! /usr/bin/perl + +use strict; + +use List::Util qw(min max); + +my @in=( + [qw(x * * * x * x x x x)], + [qw(* * * * * * * * * x)], + [qw(* * * * x * x * x *)], + [qw(* * * x x * * * * *)], + [qw(x * * * x * * * * x)], + ); + +my $ymax=$#in; +my $xmax=$#{$in[0]}; + +my @mn; +foreach (0..$ymax) { + push @mn,[(0) x ($xmax+1)]; +} + +foreach my $y (0..$ymax) { + my @sy=(max(0,$y-1)..min($ymax,$y+1)); + foreach my $x (0..$xmax) { + my @sx=(max(0,$x-1)..min($xmax,$x+1)); + if ($in[$y][$x] eq 'x') { + foreach my $yi (@sy) { + foreach my $xi (@sx) { + if ($xi==$x && $yi==$y) { + next; + } + $mn[$yi][$xi]++; + } + } + } + } +} + +foreach my $y (0..$ymax) { + foreach my $x (0..$xmax) { + if ($in[$y][$x] eq 'x') { + $mn[$y][$x]='x'; + } + } + print join(' ',@{$mn[$y]}),"\n"; +} diff --git a/challenge-126/roger-bell-west/python/ch-1.py b/challenge-126/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..d50faf6626 --- /dev/null +++ b/challenge-126/roger-bell-west/python/ch-1.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +import unittest + +def cn(n): + k=n + digits=[] + while k>0: + d=k % 10 + if d==1: + digits=[8] * len(digits) + if d>0: + d -= 1 + digits.append(d) + k=int(k/10) + tc=0 + for i in range(len(digits)-1,-1,-1): + tc *= 9 + tc += digits[i] + return tc + +class TestCn(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(cn(15),8,'example 1') + + def test_ex2(self): + self.assertEqual(cn(25),13,'example 2') + + def test_ex3(self): + self.assertEqual(cn(10000),6560,'example 3') + + def test_ex4(self): + self.assertEqual(cn(100000000),43046720,'example 3') + +unittest.main() diff --git a/challenge-126/roger-bell-west/python/ch-2.py b/challenge-126/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..f5163b5adb --- /dev/null +++ b/challenge-126/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +ina=[ + ['x','*','*','*','x','*','x','x','x','x'], + ['*','*','*','*','*','*','*','*','*','x'], + ['*','*','*','*','x','*','x','*','x','*'], + ['*','*','*','x','x','*','*','*','*','*'], + ['x','*','*','*','x','*','*','*','*','x'] +] + +ysiz=len(ina) +xsiz=len(ina[0]) + +mn=[] +for i in range(ysiz): + mn.append([0] * xsiz) + +for y in range(ysiz): + sy=range(max(0,y-1),min(ysiz,y+2)) + for x in range(xsiz): + sx=range(max(0,x-1),min(xsiz,x+2)) + if ina[y][x] == 'x': + for yi in sy: + for xi in sx: + if xi==x and yi==y: + continue + mn[yi][xi] += 1 + +for y in range(ysiz): + for x in range(xsiz): + if ina[y][x] == 'x': + mn[y][x] = 'x' + else: + mn[y][x] = str(mn[y][x]) + print(" ".join(mn[y])) diff --git a/challenge-126/roger-bell-west/raku/ch-1.p6 b/challenge-126/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..c3dc09ea03 --- /dev/null +++ b/challenge-126/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,32 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is(cn(15),8,'example 1'); +is(cn(25),13,'example 2'); +is(cn(10000),6560,'example 3'); +is(cn(100000000),43046720,'example 4'); + +sub cn($n) { + my $k=$n; + my @digits; + while ($k>0) { + my $d=$k%10; + if ($d==1) { + @digits=(8) xx @digits.elems; + } + if ($d>0) { + $d--; + } + push @digits,$d; + $k=floor($k/10); + } + my $tc=0; + for (0..@digits.end).reverse() -> $i { + $tc*=9; + $tc+=@digits[$i]; + } + return $tc; +} diff --git a/challenge-126/roger-bell-west/raku/ch-2.p6 b/challenge-126/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..3f35a4c54f --- /dev/null +++ b/challenge-126/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,43 @@ +#! /usr/bin/perl6 + +my @in=( + [qw{x * * * x * x x x x}], + [qw{* * * * * * * * * x}], + [qw{* * * * x * x * x *}], + [qw{* * * x x * * * * *}], + [qw{x * * * x * * * * x}], + ); + +my $ymax=@in.end; +my $xmax=@in[0].end; + +my @mn; +for 0..$ymax { + push @mn,[(0) xx ($xmax+1)]; +} + +for (0..$ymax) -> $y { + my @sy=(max(0,$y-1)..min($ymax,$y+1)); + for (0..$xmax) -> $x { + my @sx=(max(0,$x-1)..min($xmax,$x+1)); + if (@in[$y][$x] eq 'x') { + for @sy -> $yi { + for @sx -> $xi { + if ($xi==$x && $yi==$y) { + next; + } + @mn[$yi][$xi]++; + } + } + } + } +} + +for (0..$ymax) -> $y { + for (0..$xmax) -> $x { + if (@in[$y][$x] eq 'x') { + @mn[$y][$x]='x'; + } + } + say @mn[$y].join(' '); +} diff --git a/challenge-126/roger-bell-west/ruby/ch-1.rb b/challenge-126/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..6fc0fd84cb --- /dev/null +++ b/challenge-126/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def cn(n) + k=n + digits=[] + while (k>0) + kd=k.divmod(10) + k=kd[0] + if kd[1]==1 then + digits=Array.new(digits.length(),8) + end + if kd[1]>0 then + kd[1] -= 1 + end + digits.push(kd[1]) + end + tc=0 + (digits.length()-1).downto(0) do |i| + tc *= 9 + tc += digits[i] + end + return tc +end + +class TestPt < Test::Unit::TestCase + + def test_ex1 + assert_equal(8,cn(15)) + end + + def test_ex2 + assert_equal(13,cn(25)) + end + + def test_ex3 + assert_equal(6560,cn(10000)) + end + + def test_ex4 + assert_equal(43046720,cn(100000000)) + end + +end diff --git a/challenge-126/roger-bell-west/ruby/ch-2.rb b/challenge-126/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..44a55a8a98 --- /dev/null +++ b/challenge-126/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,40 @@ +#! /usr/bin/ruby + +ina=[ + ['x','*','*','*','x','*','x','x','x','x'], + ['*','*','*','*','*','*','*','*','*','x'], + ['*','*','*','*','x','*','x','*','x','*'], + ['*','*','*','x','x','*','*','*','*','*'], + ['x','*','*','*','x','*','*','*','*','x'] +] + +ymax=ina.length()-1 +xmax=ina[0].length()-1 + +mn=Array.new(ymax+1) {Array.new(xmax+1,0)} + +0.upto(ymax) do |y| + sy=[0,y-1].max.upto([ymax,y+1].min) + 0.upto(xmax) do |x| + sx=[0,x-1].max.upto([xmax,x+1].min) + if ina[y][x] == 'x' then + sy.each do |yi| + sx.each do |xi| + if xi==x && yi==y then + next + end + mn[yi][xi] += 1 + end + end + end + end +end + +0.upto(ymax) do |y| + 0.upto(xmax) do |x| + if ina[y][x] == "x" then + mn[y][x]="x" + end + end + print(mn[y].join(" ")+"\n") +end diff --git a/challenge-126/roger-bell-west/rust/ch-1.rs b/challenge-126/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e10f631917 --- /dev/null +++ b/challenge-126/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,44 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(cn(15),8); +} + +#[test] +fn test_ex2() { + assert_eq!(cn(25),13); +} + +#[test] +fn test_ex3() { + assert_eq!(cn(10000),6560); +} + +#[test] +fn test_ex4() { + assert_eq!(cn(100000000),43046720); +} + +fn cn(n: i64) -> i64 { + let mut k=n; + let mut digits: Vec=vec![]; + while k>0 { + let mut d=k % 10; + if d==1 { + digits=vec![8;digits.len()]; + } + if d>0 { + d -= 1; + } + digits.push(d); + k /= 10; + } + let mut tc: i64=0; + for i in (0..=digits.len()-1).rev() { + tc *= 9; + tc += digits[i]; + } + return tc; +} diff --git a/challenge-126/roger-bell-west/rust/ch-2.rs b/challenge-126/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..e9e10695d6 --- /dev/null +++ b/challenge-126/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,56 @@ +#! /bin/sh +//usr/bin/env rustc $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +fn main() { + let ina=vec![ + vec!['x','*','*','*','x','*','x','x','x','x'], + vec!['*','*','*','*','*','*','*','*','*','x'], + vec!['*','*','*','*','x','*','x','*','x','*'], + vec!['*','*','*','x','x','*','*','*','*','*'], + vec!['x','*','*','*','x','*','*','*','*','x'] + ]; + let ymax=ina.len()-1; + let xmax=ina[0].len()-1; + let mut mn: Vec>=vec![vec![0;xmax+1];ymax+1]; + for y in 0..=ymax { + for x in 0..=xmax { + if ina[y][x] == 'x' { + let mut yl=0; + if y>1 { + yl=y-1; + } + let mut yh=y+1; + if yh>ymax { + yh=ymax; + } + for yi in yl ..= yh { + let mut xl=0; + if x>1 { + xl=x-1; + } + let mut xh=x+1; + if xh>xmax { + xh=xmax; + } + for xi in xl ..= xh { + if xi==x && yi==y { + continue; + } + mn[yi][xi] += 1; + } + } + } + } + } + for y in 0..=ymax { + let mut line: Vec=vec![]; + for x in 0..=xmax { + if ina[y][x] == 'x' { + line.push("x".to_string()); + } else { + line.push(mn[y][x].to_string()); + } + } + println!("{}",line.join(" ")); + } +} -- cgit From 29508f0d381cd703ef8f160b1a1bae0ae9025d0e Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 16 Aug 2021 13:20:00 +0100 Subject: notes added --- challenge-126/james-smith/perl/ch-1.pl | 42 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl index e680e413c2..94b9ee2ff9 100644 --- a/challenge-126/james-smith/perl/ch-1.pl +++ b/challenge-126/james-smith/perl/ch-1.pl @@ -38,6 +38,19 @@ is( get_no_one_count_x($_->[0]), $_->[1] ) foreach @TESTS; is( get_no_one_count($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); +cmpthese(-5,{ 'scan 98' => sub { get_no_one_count( 98 ) }, + 'opt 98' => sub { get_no_one_count_x( 98 ) }, }); +cmpthese(-5,{ 'scan 987' => sub { get_no_one_count( 987 ) }, + 'opt 987' => sub { get_no_one_count_x( 987 ) }, }); +cmpthese(-5,{ 'scan 9876' => sub { get_no_one_count( 9876 ) }, + 'opt 9876' => sub { get_no_one_count_x( 9876 ) }, }); +cmpthese(-5,{ 'scan 98765' => sub { get_no_one_count( 98765 ) }, + 'opt 98765' => sub { get_no_one_count_x( 98765 ) }, }); +cmpthese(-5,{ 'scan 987654' => sub { get_no_one_count( 987654 ) }, + 'opt 987654' => sub { get_no_one_count_x( 987654 ) }, }); +cmpthese(-5,{ 'scan 9876543' => sub { get_no_one_count( 9876543 ) }, + 'opt 9876543' => sub { get_no_one_count_x( 9876543 ) }, }); + sub get_no_one_count { my $n = shift; return scalar grep { ! m{1} } 2..$n; @@ -45,16 +58,27 @@ sub get_no_one_count { ## Optimized version.... seems to work ... and far better than scan... sub get_no_one_count_x { - my $n = shift; - my $c = 0; - my $m = 1; + my ( $n, $count, $pow_9 ) = ( shift, 0, 1 ); while($n) { - my $t=$n%10; - $c = 0 if $t==1; - $c += $t ? ( $t < 2 ? ($m-1) : ($t-1)*$m ) : 0; - $m *= 9; - $n= int($n/10); + my $t = $n % 10; ## get last digit + $count = 0 if $t==1; ## Throw everything away we've found a 1 + $count += $t ? ( $t == 1 ? ($pow_9-1) : ($t-1)*$pow_9 ) : 0; + ## 0 it contributes nothing + ## 1 contributes 9^X-1 + ## 2-9 contributes (n-1)9^X + $pow_9 *= 9; ## update power of nine + $n = ( $n - $t )/10; ## drop last digit } - return $c; + return $count; } +## Comparison + +# | N | scan | opt | Speed-up | +# | --------: | --------: | --------: | ---------: | +# | 98 | 16,027 | 1,173,850 | 72 | +# | 987 | 2,623 | 867,796 | 330 | +# | 9,876 | 253 | 685,956 | 2,715 | +# | 98,765 | 24.4 | 565,427 | 23,155 | +# | 987,654 | 1.23 | 482,800 | 392,998 | +# | 9,876,543 | 0.23 | 418,410 | 1,853,771 | -- cgit From f9896cfb012e307db009089aba9c24c35368fb06 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 16 Aug 2021 14:29:17 +0100 Subject: d --- challenge-126/james-smith/README.md | 109 +++++++++++++++++++++--------------- challenge-126/james-smith/blog.txt | 1 + 2 files changed, 65 insertions(+), 45 deletions(-) create mode 100644 challenge-126/james-smith/blog.txt diff --git a/challenge-126/james-smith/README.md b/challenge-126/james-smith/README.md index 97ebd934cd..339d6f4d9d 100644 --- a/challenge-126/james-smith/README.md +++ b/challenge-126/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #125 +# Perl Weekly Challenge #126 You can find more information about this weeks, and previous weeks challenges at: @@ -10,67 +10,86 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-125/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-126/james-smith/perl -# Task 1 - Pythagorean Triples +# Task 1 - Count Numbers -***You are given a positive integer `$N`. Write a script to print all Pythagorean Triples containing `$N` as a member. Print `-1` if it can’t be a member of any. Triples with the same set of elements are considered the same, i.e. if your script has already printed (3, 4, 5), (4, 3, 5) should not be printed.*** +***You are given a positive integer `$N` - Write a script to print count of numbers from `1` to `$N` that don’t contain digit `1`.*** ## The solution -There are two cases to consider - whether `$N` is the hypotenuse or one of the shorter sides. +First pass - just do the simple `O(n)` thing which is to loop through all `$N` numbers and count those without `1`s. - * `$N` is the hypotenuse - we just need to work out whether `$N**2 - $a**2` is a square for `$a` between `3` and `$N/sqrt 2` +```perl +sub get_no_one_count { + my $n = shift; + return scalar grep { ! m{1} } 2..$n; +} +``` - * `$N` is not the hypotenuse - we need to loop through `$a` from `$N+1` such that `$a**2-$N**2` is a square. +If we look at a few numbers we see that for powers of 10 we see we have values 8, 80, 728, 6560, 59048, ... what we notices these are all of the form `9^$N-1`.... For multiples of these we see that for the total is `(n-1)*9^N`. + +We see we can then add these up - with one exception if we find a 1 we stop the loop (or if working backwards) throw any calculations away - giving us the order `O(ln n)` solution: -This gives: ```perl -sub get_triples { - my $n = shift; - return $n < 3 ? -1 : join '; ', map { sprintf '(%s)', join ', ', @{$_} } - ( - grep { $_->[1] == int $_->[1] } ## Check if all int - map { [ $_, sqrt($n**2-$_**2), $n ] } ## Generate triple - 3 .. sqrt($n**2/2) ## Shortest side ($n is hypotenuse) - ),( - map { $_->[0]>$_->[1] ? [@{$_}[1,0,2]] : $_ } ## put in numerical order - grep { $_->[1] == int $_->[1] } ## Check all int - map { [ $n, sqrt($_**2-$n**2), $_ ] } ## Generate triple - ($n+1) .. ($n**2/2+1) ## Hypotenuse ($n is one of other two sides) - ); +sub get_no_one_count_9 { + my ( $n, $count, $pow_9 ) = ( shift, 0, 1 ); + while($n) { + my $t = $n % 10; ## get last digit + $count = 0 if $t==1; ## Throw everything away we've found a 1 + $count += $t ? ( $t == 1 ? ($pow_9-1) : ($t-1)*$pow_9 ) : 0; + ## 0 it contributes nothing + ## 1 contributes 9^X-1 + ## 2-9 contributes (n-1)9^X + $pow_9 *= 9; ## update power of nine + $n = ( $n - $t )/10; ## drop last digit + } + return $count; } ``` -# Task 2 - Binary Tree Diameter -*** Write a script to find the diameter of the given binary tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. It doesn’t have to pass through the root.*** +## Comparison +Even for 2 digit numbers this speed up is good (72x), but as N increases +we see that gain gets higher and higher.. for 7 digit numbers the speed +up is 6 orders of magnitude. -## Solution +| N | scan | opt | Speed-up | +| --------: | --------: | --------: | ---------: | +| 98 | 16,027 | 1,173,850 | 72 | +| 987 | 2,623 | 867,796 | 330 | +| 9,876 | 253 | 685,956 | 2,715 | +| 98,765 | 24.4 | 565,427 | 23,155 | +| 987,654 | 1.23 | 482,800 | 392,998 | +| 9,876,543 | 0.23 | 418,410 | 1,853,771 | -For any node - we can compute the longest tree which goes through the node itself - this is the sum of the maximum lengths of the left tree and the depth of the right. We do know that there will be trees for which this is not the diameter - there could be another node for which the left and right depths sum to a larger value... +# Task 2 - Minesweeper Game -So to compute the diameter of the tree we just choose the maximum value of the maximum lengths of the left/right sub tree. +***You are given a rectangle with points marked with either `x` or `*`. Please consider the `x` as a land mine. Write a script to print a rectangle with numbers and `x` as in the Minesweeper game.*** -We will re-use the BinaryTree module from a previous challenge and so need to define walk functions to work out the maximum length of a subtree and consequently diameter... +## Solution ```perl -sub max_length { - my $self = shift; - my $d = 0; - $d = $self->left->max_length if $self->has_left; - return 1+$d unless $self->has_right; - my $t = $self->right->max_length; - return $t > $d ? 1+$t : 1+$d; -} - -sub diameter { - my $self = shift; - my $global = { 'diameter' => 0 }; - $self->walk( sub { - my $d = ($_[0]->has_left ? $_[0]->left->max_length : 0 ) + - ($_[0]->has_right ? $_[0]->right->max_length : 0 ); - $_[1]{'diameter'} = $d if $d > $_[1]->{'diameter'}; - }, $global ); - return $global->{'diameter'}; +sub solve { + my @res = (''); + ## Map input of strings into an array of 1s (bombs) + 0s (spaces) + my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_; + + ## Get index of last row or column... + my( $h, $w ) = ( $#_, -1 + length $_[0] ); + + foreach my $y ( 0 .. $h ) { + $res[-1] .= $in[$y][$_] ? 'x' : + ( $y ? ( $_ ? $in[$y-1][$_-1] : 0 ) + $in[$y-1][$_] + ( $_ < $w ? $in[$y-1][$_+1] : 0 ) : 0 ) + + ( $_ ? $in[$y ][$_-1] : 0 ) + $in[$y ][$_] + ( $_ < $w ? $in[$y ][$_+1] : 0 ) + + ( $y < $h ? ( $_ ? $in[$y+1][$_-1] : 0 ) + $in[$y+1][$_] + ( $_ < $w ? $in[$y+1][$_+1] : 0 ) : 0 ) foreach 0 .. $w; + push @res, ''; + } + return join "\n", @res; } ``` + +There are two stages to this: + + 1. convert the strings into an array of `1`s and `0`s representing mines and spaces. + 2. we compute the convulation - counting the number of mines in adjacent squares (or tagging with a "x" if the square is bomb) + diff --git a/challenge-126/james-smith/blog.txt b/challenge-126/james-smith/blog.txt new file mode 100644 index 0000000000..7562bfe1b6 --- /dev/null +++ b/challenge-126/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-126/james-smith \ No newline at end of file -- cgit From 1a1b004f55fb895b19d1e09528e4834f3bfbf9ef Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 16 Aug 2021 14:29:50 +0100 Subject: change name --- challenge-126/james-smith/perl/ch-1.pl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl index 94b9ee2ff9..b3a1866f84 100644 --- a/challenge-126/james-smith/perl/ch-1.pl +++ b/challenge-126/james-smith/perl/ch-1.pl @@ -32,24 +32,24 @@ my @TESTS = ( [ 1000000, 531440 ], ); -# warn "@{$_} -> ", get_no_one_count_x($_->[0]), "\n" foreach @TESTS; +# warn "@{$_} -> ", get_no_one_count_9($_->[0]), "\n" foreach @TESTS; -is( get_no_one_count_x($_->[0]), $_->[1] ) foreach @TESTS; +is( get_no_one_count_9($_->[0]), $_->[1] ) foreach @TESTS; is( get_no_one_count($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); cmpthese(-5,{ 'scan 98' => sub { get_no_one_count( 98 ) }, - 'opt 98' => sub { get_no_one_count_x( 98 ) }, }); + 'opt 98' => sub { get_no_one_count_9( 98 ) }, }); cmpthese(-5,{ 'scan 987' => sub { get_no_one_count( 987 ) }, - 'opt 987' => sub { get_no_one_count_x( 987 ) }, }); + 'opt 987' => sub { get_no_one_count_9( 987 ) }, }); cmpthese(-5,{ 'scan 9876' => sub { get_no_one_count( 9876 ) }, - 'opt 9876' => sub { get_no_one_count_x( 9876 ) }, }); + 'opt 9876' => sub { get_no_one_count_9( 9876 ) }, }); cmpthese(-5,{ 'scan 98765' => sub { get_no_one_count( 98765 ) }, - 'opt 98765' => sub { get_no_one_count_x( 98765 ) }, }); + 'opt 98765' => sub { get_no_one_count_9( 98765 ) }, }); cmpthese(-5,{ 'scan 987654' => sub { get_no_one_count( 987654 ) }, - 'opt 987654' => sub { get_no_one_count_x( 987654 ) }, }); + 'opt 987654' => sub { get_no_one_count_9( 987654 ) }, }); cmpthese(-5,{ 'scan 9876543' => sub { get_no_one_count( 9876543 ) }, - 'opt 9876543' => sub { get_no_one_count_x( 9876543 ) }, }); + 'opt 9876543' => sub { get_no_one_count_9( 9876543 ) }, }); sub get_no_one_count { my $n = shift; @@ -57,7 +57,7 @@ sub get_no_one_count { } ## Optimized version.... seems to work ... and far better than scan... -sub get_no_one_count_x { +sub get_no_one_count_9 { my ( $n, $count, $pow_9 ) = ( shift, 0, 1 ); while($n) { my $t = $n % 10; ## get last digit -- cgit From ca5cd9c05d9a0e6aacc064404b6beafcd241995e Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 16 Aug 2021 23:58:20 +1000 Subject: sgreen solution to challenge 126 --- challenge-126/sgreen/README.md | 4 +-- challenge-126/sgreen/blog.txt | 1 + challenge-126/sgreen/perl/ch-1.pl | 24 +++++++++++++++ challenge-126/sgreen/perl/ch-2.pl | 64 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 challenge-126/sgreen/blog.txt create mode 100755 challenge-126/sgreen/perl/ch-1.pl create mode 100755 challenge-126/sgreen/perl/ch-2.pl diff --git a/challenge-126/sgreen/README.md b/challenge-126/sgreen/README.md index e025103f4b..f9b8c29dad 100644 --- a/challenge-126/sgreen/README.md +++ b/challenge-126/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 124 +# The Weekly Challenge 126 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-124-2gi7) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-126-19fg) diff --git a/challenge-126/sgreen/blog.txt b/challenge-126/sgreen/blog.txt new file mode 100644 index 0000000000..6f1f01dfd6 --- /dev/null +++ b/challenge-126/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-126-19fg diff --git a/challenge-126/sgreen/perl/ch-1.pl b/challenge-126/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..af1c8d6326 --- /dev/null +++ b/challenge-126/sgreen/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $N = shift; + + # Sanity check + die "You must supply a postive integer" unless defined $N; + die "The value '$N' is not a positive integer\n" unless $N =~ /^[1-9][0-9]*$/; + + # Count the number of numbers that don't have a one + my $count = 0; + foreach my $number ( 2 .. $N ) { + ++$count if index( $number, '1' ) == -1; + } + + # Display the result + say $count; +} + +main(@ARGV); diff --git a/challenge-126/sgreen/perl/ch-2.pl b/challenge-126/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c00bd75489 --- /dev/null +++ b/challenge-126/sgreen/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _process_input { + my @rows = @_; + my @cells = (); + + # Convert rows of x and * to an array of arrays + foreach my $row (<>) { + + # Skip empty rows + next if $row =~ /^\s+$/; + push @cells, [ $row =~ /([x\*])/gm ]; + } + + # Check we have at least one row + die "You must specify at least one row\n" unless scalar(@cells); + + # Check all rows are the same length + foreach my $i ( 1 .. $#cells ) { + die "Not all rows are the same length\n" if scalar( @{ $cells[0] } ) != scalar( @{ $cells[$i] } ); + } + + return @cells; +} + +sub main { + # Unlike most tasks, we will read the input from STDIN. + my @cells = _process_input(); + + # All movements, clockwise from up left + my @directions = ( [ -1, -1 ], [ 0, -1 ], [ 1, -1 ], [ 1, 0 ], [ 1, 1 ], [ 0, 1 ], [ -1, 1 ], [ -1, 0 ] ); + + my $rows = $#cells; + my $cols = $#{ $cells[0] }; + + # Inspect each cells + foreach my $row ( 0 .. $rows ) { + foreach my $col ( 0 .. $cols ) { + # Skip cells that have mines + next if $cells[$row][$col] eq 'x'; + + # Count the number of surronding mines if the are within the + # bounds of the board + my $mines = 0; + foreach my $delta (@directions) { + my $x = $row + $delta->[0]; + my $y = $col + $delta->[1]; + ++$mines if $x >= 0 and $x <= $rows and $y >= 0 and $y <= $cols and $cells[$x][$y] eq 'x'; + } + $cells[$row][$col] = $mines; + } + } + + # Display the results + foreach my $rows (@cells) { + say join ' ', @$rows; + } +} + +main(); -- cgit From cf9af05bb020926b7563a3bb40faa41c480a5eb6 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 16 Aug 2021 23:58:44 +1000 Subject: no solution to challenge 125 --- challenge-125/sgreen/README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 challenge-125/sgreen/README.md diff --git a/challenge-125/sgreen/README.md b/challenge-125/sgreen/README.md deleted file mode 100644 index e025103f4b..0000000000 --- a/challenge-125/sgreen/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# The Weekly Challenge 124 - -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-124-2gi7) -- cgit From da1bb5e2811e7febac224fc576b34ae6e07d5c3e Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 16 Aug 2021 16:11:08 +0100 Subject: add solution week 126 task 1 --- challenge-126/steven-wilson/perl/ch-01.pl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-126/steven-wilson/perl/ch-01.pl diff --git a/challenge-126/steven-wilson/perl/ch-01.pl b/challenge-126/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..858b161f29 --- /dev/null +++ b/challenge-126/steven-wilson/perl/ch-01.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw/ say /; + +my $input = $ARGV[0]; +my $count = 0; +my $current_number = 2; + +if ( not defined $input ) { + die "Enter command line argument as input\n"; +} +elsif ( not( $input =~ /\A[1-9][0-9]*\z/ ) ) { + die "Input should be positive integer\n"; +} + +while ( $current_number <= $input ) { + if ( not( $current_number =~ /1/ ) ) { + $count++; + } + $current_number++; +} + +say $count; -- cgit From 30aae9e73fc2375388125bd03d88b01d6cbe9d92 Mon Sep 17 00:00:00 2001 From: Cris-HD Date: Mon, 16 Aug 2021 20:14:42 +0200 Subject: Added challenge 126 solution --- challenge-126/cristian-heredia/perl/ch_1.pl | 36 +++++++++++++++++++++++++++ challenge-126/cristian-heredia/python/ch_1.py | 30 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100755 challenge-126/cristian-heredia/perl/ch_1.pl create mode 100755 challenge-126/cristian-heredia/python/ch_1.py diff --git a/challenge-126/cristian-heredia/perl/ch_1.pl b/challenge-126/cristian-heredia/perl/ch_1.pl new file mode 100755 index 0000000000..d70f4756fd --- /dev/null +++ b/challenge-126/cristian-heredia/perl/ch_1.pl @@ -0,0 +1,36 @@ +=begin + TASK #1 › Count Numbers + Submitted by: Mohammad S Anwar + You are given a positive integer $N. + + Write a script to print count of numbers from 1 to $N that don’t contain digit 1. + + Example + Input: $N = 15 + Output: 8 + + There are 8 numbers between 1 and 15 that don't contain digit 1. + 2, 3, 4, 5, 6, 7, 8, 9. + + Input: $N = 25 + Output: 13 + + There are 13 numbers between 1 and 25 that don't contain digit 1. + 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25. + +=end +=cut + +use strict; +use warnings; +use Data::Dumper; + +my $N = 15; +my $counter = 0; + +for my $iter (1 .. $N) { + if ( $iter !~ /1+/) { + $counter++; + } +} +print("Output: $counter\n"); \ No newline at end of file diff --git a/challenge-126/cristian-heredia/python/ch_1.py b/challenge-126/cristian-heredia/python/ch_1.py new file mode 100755 index 0000000000..f0ec59c383 --- /dev/null +++ b/challenge-126/cristian-heredia/python/ch_1.py @@ -0,0 +1,30 @@ +''' + TASK #1 › Count Numbers + Submitted by: Mohammad S Anwar + You are given a positive integer $N. + + Write a script to print count of numbers from 1 to $N that don’t contain digit 1. + + Example + Input: $N = 15 + Output: 8 + + There are 8 numbers between 1 and 15 that don't contain digit 1. + 2, 3, 4, 5, 6, 7, 8, 9. + + Input: $N = 25 + Output: 13 + + There are 13 numbers between 1 and 25 that don't contain digit 1. + 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25. + +''' + +N = 15 +counter = 0 + +for iter in range(1, N+1): + if not '1' in str(iter): + counter += 1 + +print(f"Output: {counter}"); \ No newline at end of file -- cgit From c21a68248f28cfb1a9fc9b5022e3524e9c47b359 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 16 Aug 2021 21:15:43 +0100 Subject: - Added solution by Scimon Proctor. --- stats/pwc-challenge-125.json | 453 +++ stats/pwc-current.json | 454 +-- stats/pwc-language-breakdown-summary.json | 54 +- stats/pwc-language-breakdown.json | 4963 +++++++++++++++-------------- stats/pwc-leaders.json | 756 ++--- stats/pwc-summary-1-30.json | 94 +- stats/pwc-summary-121-150.json | 110 +- stats/pwc-summary-151-180.json | 106 +- stats/pwc-summary-181-210.json | 46 +- stats/pwc-summary-211-240.json | 110 +- stats/pwc-summary-31-60.json | 46 +- stats/pwc-summary-61-90.json | 36 +- stats/pwc-summary-91-120.json | 46 +- stats/pwc-summary.json | 40 +- 14 files changed, 3700 insertions(+), 3614 deletions(-) create mode 100644 stats/pwc-challenge-125.json diff --git a/stats/pwc-challenge-125.json b/stats/pwc-challenge-125.json new file mode 100644 index 0000000000..def2c1a761 --- /dev/null +++ b/stats/pwc-challenge-125.json @@ -0,0 +1,453 @@ +{ + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "series" : [ + { + "name" : "The Weekly Challenge - 125", + "colorByPoint" : 1, + "data" : [ + { + "drilldown" : "Abigail", + "name" : "Abigail", + "y" : 4 + }, + { + "name" : "Arne Sommer", + "y" : 3, + "drilldown" : "Arne Sommer" + }, + { + "y" : 2, + "name" : "Cheok-Yin Fung", + "drilldown" : "Cheok-Yin Fung" + }, + { + "name" : "Colin Crain", + "y" : 5, + "drilldown" : "Colin Crain" + }, + { + "drilldown" : "Duncan C. White", + "name" : "Duncan C. White", + "y" : 2 + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "drilldown" : "Flavio Poletti", + "y" : 6, + "name" : "Flavio Poletti" + }, + { + "y" : 3, + "name" : "James Smith", + "drilldown" : "James Smith" + }, + { + "y" : 1, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "name" : "Joan Mimosinnet", + "y" : 2, + "drilldown" : "Joan Mimosinnet" + }, + { + "name" : "Jorg Sommrey", + "y" : 2, + "drilldown" : "Jorg Sommrey" + }, + { + "y" : 1, + "name" : "Kai Burgdorf", + "drilldown" : "Kai Burgdorf" + }, + { + "drilldown" : "Laurent Rosenfeld", + "y" : 3, + "name" : "Laurent Rosenfeld" + }, + { + "y" : 2, + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson" + }, + { + "drilldown" : "Matthew Neleigh", + "y" : 2, + "name" : "Matthew Neleigh" + }, + { + "drilldown" : "Pete Houston", + "y" : 1, + "name" : "Pete Houston" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 1, + "name" : "Peter Campbell Smith" + }, + { + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", + "y" : 4 + }, + { + "y" : 2, + "name" : "Simon Proctor", + "drilldown" : "Simon Proctor" + }, + { + "drilldown" : "Stuart Little", + "y" : 4, + "name" : "Stuart Little" + }, + { + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "y" : 2 + }, + { + "y" : 3, + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" + }, + { + "name" : "Wanderdoc", + "y" : 1, + "drilldown" : "Wanderdoc" + } + ] + } + ], + "chart" : { + "type" : "column" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "subtitle" : { + "text" : "[Champions: 23] Last updated at 2021-08-16 20:14:35 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "tooltip" : { + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
", + "followPointer" : 1 + }, + "title" : { + "text" : "The Weekly Challenge - 125" + }, + "legend" : { + "enabled" : 0 + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Abigail", + "name" : "Abigail" + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Arne Sommer", + "id" : "Arne Sommer" + }, + { + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Colin Crain", + "name" : "Colin Crain", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Duncan C. White", + "name" : "Duncan C. White" + }, + { + "name" : "E. Choroba", + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Flavio Poletti", + "name" : "Flavio Poletti" + }, + { + "name" : "James Smith", + "id" : "James Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 1 + ] + ], + "name" : "Jan Krnavek", + "id" : "Jan Krnavek" + }, + { + "id" : "Joan Mimosinnet", + "name" : "Joan Mimosinnet", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 1 + ] + ], + "name" : "Kai Burgdorf", + "id" : "Kai Burgdorf" + }, + { + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" + }, + { + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Pete Houston", + "name" : "Pete Houston", + "data" : [ + [ + "Perl", + 1 + ] + ] + }, + { + "name" : "Peter Campbell Smith", + "id" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 1 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Simon Proctor", + "name" : "Simon Proctor" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Stuart Little", + "id" : "Stuart Little" + }, + { + "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ] + }, + { + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Wanderdoc", + "id" : "Wanderdoc", + "data" : [ + [ + "Perl", + 1 + ] + ] + } + ] + } +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 25520e28d2..9db4f53363 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,151 +1,11 @@ { - "subtitle" : { - "text" : "[Champions: 23] Last updated at 2021-08-16 12:03:03 GMT" - }, - "tooltip" : { - "headerFormat" : "{series.name}
", - "pointFormat" : "{point.name}: {point.y:f}
", - "followPointer" : 1 - }, - "series" : [ - { - "colorByPoint" : 1, - "data" : [ - { - "y" : 4, - "name" : "Abigail", - "drilldown" : "Abigail" - }, - { - "y" : 3, - "drilldown" : "Arne Sommer", - "name" : "Arne Sommer" - }, - { - "name" : "Cheok-Yin Fung", - "drilldown" : "Cheok-Yin Fung", - "y" : 2 - }, - { - "drilldown" : "Colin Crain", - "name" : "Colin Crain", - "y" : 5 - }, - { - "name" : "Duncan C. White", - "drilldown" : "Duncan C. White", - "y" : 2 - }, - { - "y" : 2, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" - }, - { - "name" : "Flavio Poletti", - "drilldown" : "Flavio Poletti", - "y" : 6 - }, - { - "drilldown" : "James Smith", - "name" : "James Smith", - "y" : 3 - }, - { - "name" : "Jan Krnavek", - "drilldown" : "Jan Krnavek", - "y" : 1 - }, - { - "name" : "Joan Mimosinnet", - "drilldown" : "Joan Mimosinnet", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey" - }, - { - "name" : "Kai Burgdorf", - "drilldown" : "Kai Burgdorf", - "y" : 1 - }, - { - "drilldown" : "Laurent Rosenfeld", - "name" : "Laurent Rosenfeld", - "y" : 3 - }, - { - "y" : 2, - "name" : "Mark Anderson", - "drilldown" : "Mark Anderson" - }, - { - "name" : "Matthew Neleigh", - "drilldown" : "Matthew Neleigh", - "y" : 2 - }, - { - "drilldown" : "Pete Houston", - "name" : "Pete Houston", - "y" : 1 - }, - { - "drilldown" : "Peter Campbell Smith", - "name" : "Peter Campbell Smith", - "y" : 1 - }, - { - "drilldown" : "Roger Bell_West", - "name" : "Roger Bell_West", - "y" : 4 - }, - { - "drilldown" : "Simon Proctor", - "name" : "Simon Proctor", - "y" : 2 - }, - { - "y" : 4, - "drilldown" : "Stuart Little", - "name" : "Stuart Little" - }, - { - "y" : 2, - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke" - }, - { - "y" : 3, - "name" : "W. Luis Mochan", - "drilldown" : "W. Luis Mochan" - }, - { - "y" : 1, - "drilldown" : "Wanderdoc", - "name" : "Wanderdoc" - } - ], - "name" : "The Weekly Challenge - 125" - } - ], - "xAxis" : { - "type" : "category" - }, - "title" : { - "text" : "The Weekly Challenge - 125" - }, - "chart" : { - "type" : "column" - }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "format" : "{point.y}", "enabled" : 1 - } + }, + "borderWidth" : 0 } }, "legend" : { @@ -153,114 +13,6 @@ }, "drilldown" : { "series" : [ - { - "name" : "Abigail", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 2 - ] - ], - "id" : "Abigail" - }, - { - "data" : [ - [ - "Raku", - 2 - ], - [ - "Blog", - 1 - ] - ], - "id" : "Arne Sommer", - "name" : "Arne Sommer" - }, -