From 259ce124ace36cff23513157b49ecafb9d0da1fd Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 08:53:30 +0100 Subject: Create ch-1.pl --- challenge-210/james-smith/perl/ch-1.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-210/james-smith/perl/ch-1.pl diff --git a/challenge-210/james-smith/perl/ch-1.pl b/challenge-210/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..12f42c9fc4 --- /dev/null +++ b/challenge-210/james-smith/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [2,3,1], 6 ], + [ [1,1,2,2,2,3], 11 ], +); + +sub kill_and_win { + my($max,%v,$x)=0; + $v{$_}++ for @_; ## Get freq in hash + ( ( $x = ( $v{$_-1} ? $v{$_-1}*($_-1) : 0 ) ## Compute value + + ( $v{$_ } ? $v{$_ }* $_ : 0 ) ## for current + + ( $v{$_+1} ? $v{$_+1}*($_+1) : 0 ) ## integer + ) > $max ) && ($max = $x) for keys %v; ## if max reset max + $max ## return value +} + +is( kill_and_win( @{$_->[0]} ), $_->[1] ) for @TESTS; -- cgit From 029680e3829388f61ba89cef6ec43f1cff14ba98 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 11:27:34 +0100 Subject: Create ch-2.pl --- challenge-210/james-smith/perl/ch-2.pl | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-210/james-smith/perl/ch-2.pl diff --git a/challenge-210/james-smith/perl/ch-2.pl b/challenge-210/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..495cfe3845 --- /dev/null +++ b/challenge-210/james-smith/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [2,3,-1] => '2 3' ], + [ [3,2,-4] => '-4' ], + [ [1,-1] => '' ], +); + +sub collision { + my @st; + $_[0]>0 || !@st || $st[-1] < 0 ? ( push @st, shift ) + ## +ve no, empty stack or last stack is -ve + ## we keep this at the moment so push to stack + : $st[-1] == -$_[0] ? ( pop @st, shift ) + ## -ve no and equal in absolute value + ## remove +ve value from stack and drop + ## current value + : $st[-1] >= -$_[0] ? ( shift ) + ## -ve no and smaller in abs value + ## drop current value + : ( pop @st ) + ## -ve no and greater in abs value + ## remove previous number from stack + while @_; + @st ## return value +} + +is( "@{[ collision( @{$_->[0]} ) ]}", $_->[1] ) for @TESTS; + +## Without comments... + +sub nc_collision { + my @st; + $_[0]>0 || !@st || $st[-1] < 0 ? push @st, shift + : $st[-1] == -$_[0] ? pop @st && shift + : $st[-1] >= -$_[0] ? shift + : pop @st + while @_; + @st +} -- cgit From 36cd598745ba494ac161afd0ebabe8936d3f77ca Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 11:44:04 +0100 Subject: Update ch-2.pl --- challenge-210/james-smith/perl/ch-2.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-210/james-smith/perl/ch-2.pl b/challenge-210/james-smith/perl/ch-2.pl index 495cfe3845..8d0c1479a8 100644 --- a/challenge-210/james-smith/perl/ch-2.pl +++ b/challenge-210/james-smith/perl/ch-2.pl @@ -13,17 +13,17 @@ my @TESTS = ( sub collision { my @st; - $_[0]>0 || !@st || $st[-1] < 0 ? ( push @st, shift ) + $_[0]>0 || !@st || $st[-1] < 0 ? push @st, shift ## +ve no, empty stack or last stack is -ve ## we keep this at the moment so push to stack - : $st[-1] == -$_[0] ? ( pop @st, shift ) + : $st[-1] == -$_[0] ? pop @st && shift ## -ve no and equal in absolute value ## remove +ve value from stack and drop ## current value - : $st[-1] >= -$_[0] ? ( shift ) + : $st[-1] >= -$_[0] ? shift ## -ve no and smaller in abs value ## drop current value - : ( pop @st ) + : pop @st ## -ve no and greater in abs value ## remove previous number from stack while @_; -- cgit From 30bcfaa6f34d371264dec6a72afe796106eecc87 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 12:55:17 +0100 Subject: Create blog.txt --- challenge-210/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-210/james-smith/blog.txt diff --git a/challenge-210/james-smith/blog.txt b/challenge-210/james-smith/blog.txt new file mode 100644 index 0000000000..37fd81f994 --- /dev/null +++ b/challenge-210/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-210/james-smith/blog.txt -- cgit From 706737f15c4a18ef44e26c080508b28240917fe7 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 27 Mar 2023 23:51:44 +1100 Subject: Simon's solution to challenge 210 --- challenge-210/sgreen/README.md | 2 +- challenge-210/sgreen/blog.txt | 1 + challenge-210/sgreen/perl/ch-1.pl | 28 ++++++++++++++++++++++++++ challenge-210/sgreen/perl/ch-2.pl | 40 +++++++++++++++++++++++++++++++++++++ challenge-210/sgreen/python/ch-1.py | 25 +++++++++++++++++++++++ challenge-210/sgreen/python/ch-2.py | 39 ++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 challenge-210/sgreen/blog.txt create mode 100755 challenge-210/sgreen/perl/ch-1.pl create mode 100755 challenge-210/sgreen/perl/ch-2.pl create mode 100755 challenge-210/sgreen/python/ch-1.py create mode 100755 challenge-210/sgreen/python/ch-2.py diff --git a/challenge-210/sgreen/README.md b/challenge-210/sgreen/README.md index f393339e11..64a534294d 100644 --- a/challenge-210/sgreen/README.md +++ b/challenge-210/sgreen/README.md @@ -1,3 +1,3 @@ # The Weekly Challenge 209 -Blog: [Special Accounts](https://dev.to/simongreennet/special-accounts-5969) +Blog: [Numbers Challenges](https://dev.to/simongreennet/numbers-challenges-32k1) diff --git a/challenge-210/sgreen/blog.txt b/challenge-210/sgreen/blog.txt new file mode 100644 index 0000000000..295399d982 --- /dev/null +++ b/challenge-210/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/numbers-challenges-32k1 \ No newline at end of file diff --git a/challenge-210/sgreen/perl/ch-1.pl b/challenge-210/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..0e865f4b13 --- /dev/null +++ b/challenge-210/sgreen/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(sum uniq); + +sub main(@array) { + my $score = 0; + + # Get all unique numbers + foreach my $i (uniq(@array)) { + # Calculate the sum of all numbers one less, the same or one more + # than the target + my $this_score = sum( grep { $_ >= $i-1 and $_ <= $i+1} @array); + + # Record this score if it is larger + if ($score < $this_score) { + $score = $this_score; + } + } + + say $score; +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-210/sgreen/perl/ch-2.pl b/challenge-210/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..45e1ebb835 --- /dev/null +++ b/challenge-210/sgreen/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@array) { + # Continue until we can't + C: while (1) { + # Go through each item in the array, get that value and the next + foreach my $i ( 0 .. $#array - 1 ) { + my $left = $array[$i]; + my $right = $array[ $i + 1 ]; + + # If the left value is > 0 and the right one is < 0, we have + # a collision + if ( $left > 0 and $right < 0 ) { + $right = abs($right); + if ( $right <= $left ) { + # Remove the right number + splice( @array, $i + 1, 1 ); + } + if ( $right >= $left ) { + # Remove the left number + splice( @array, $i, 1 ); + } + + # Start the loop again + next C; + } + } + + # We've gone through the list and removed all collisions + last; + } + + say '(', join( ', ', @array ), ')'; +} +main(@ARGV); \ No newline at end of file diff --git a/challenge-210/sgreen/python/ch-1.py b/challenge-210/sgreen/python/ch-1.py new file mode 100755 index 0000000000..6ec294ffce --- /dev/null +++ b/challenge-210/sgreen/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import sys + + +def main(array): + score = 0 + + # Get all unique numbers + for i in set(array): + # Calculate the sum of all numbers one less, the same or one more + # than the target + this_score = sum(x for x in array if i-1 <= x <= i+1) + + # Record this score if it is larger + if score < this_score: + score = this_score + + print(score) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-210/sgreen/python/ch-2.py b/challenge-210/sgreen/python/ch-2.py new file mode 100755 index 0000000000..791d842c48 --- /dev/null +++ b/challenge-210/sgreen/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import sys + + +def main(array): + # Continue until we can't + should_continue = True + + while should_continue: + # Go through each item in the array, get that value and the next + for i in range(len(array)-1): + left = array[i] + right = array[i+1] + + # If the left value is > 0 and the right one is < 0, we have + # a collision + if left > 0 and right < 0: + right = abs(right) + if right <= left: + # Remove the right number + array.pop(i+1) + if right >= left: + # Remove the left number + array.pop(i) + + # Start the loop again + break + else: + # We've gone through the list and removed all collisions + should_continue = False + + print(array) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) -- cgit From 7725d181335437741ebe50604ce3272163744174 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 14:20:14 +0100 Subject: Update README.md --- challenge-210/james-smith/README.md | 96 +++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 53 deletions(-) diff --git a/challenge-210/james-smith/README.md b/challenge-210/james-smith/README.md index 47a6bbcd83..ee869efe5b 100644 --- a/challenge-210/james-smith/README.md +++ b/challenge-210/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 208](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-209/james-smith) | -[Next 210 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-210/james-smith) +[< Previous 209](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-209/james-smith) | +[Next 211 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-211/james-smith) -# ED-209: The Weekly Challenge +# The Weekly Challenge 210 You can find more information about this weeks, and previous weeks challenges at: @@ -13,77 +13,67 @@ 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-209/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-210/james-smith -# Task 1: Special Bit Characters +# Task 1: Kill and Win -***You are given an array of binary bits that ends with `0`. Valid sequences in the bit string are: -`[0]` decodes to `"a"`, `[1, 0]` to `"b"`, `[1, 1]` to `"c"`. Write a script to print `1` if the last character is an `"a"` otherwise print `0`.*** +***You are given a list of integers. Write a script to get the maximum points. You are allowed to take out (kill) any integer and remove from the list. However if you do that then all integers exactly one-less or one-more would also be removed. Find out the total of integers removed.*** ## Solution -Firstly we quickly check the last entry is a `0` if it isn't we return `-1` "error". We then look through each bit in turn - if it is `0` we go to the next bit; if it is a `1` we skip one bit and go to the next. We loop through to the end of the array. We loop till the array has `0` or `1` elements left - if we have one element left then the last character is an "a" if we have none it is not. So we can just return scalar @a`. -```perl -sub special_bit_chars { - return 0 if $_[-1]; - ($_[0]&&shift), shift until @_<2; - scalar @_ -} -``` -## Solution 2 +Although it's not fully clear on the rules - we make the assumption that it means once we chose one number all those above and below are killed. -As well as tracking from the front we can track from the back. +Firstly we compute counts of each of the numbers, we then for each value compute: -First we need to note: +``` + count{n-1}*(n-1) + count{n}*n + count{n+1}*(n+1) +``` - * Last character must be a `0` - * If there is string ending in a `0` then we can ignore anything up to this, as `0` is always at the right hand character in a string; - * Additionally if the last two characters are 0 then we know that the answer is true. - * So breaking this down we need to work out whether the value is true or false if the list ends: `.....,1,0`. If the string consists of series of `n` pairs of `1`s then this converts to "...CCA" and so the last character is `A` so we return 0; If it is an odd number of 1s we have the string "...CCB" so the return value is false. +We just loop through all the keys of the count and compute the maximum value: ```perl -sub special_bit_chars_reverse { - my$f,pop?return 0:pop||return 1; - $f++,pop||last while@_; - 1&$f +sub kill_and_win { + my($m,%c,$x)=0; + $c{$_}++ for @_; ## Get freq in hash + ( ( $x = ( $c{$_-1} ? $c{$_-1}*($_-1) : 0 ) ## Compute value + + ( $c{$_ } ? $c{$_ }* $_ : 0 ) ## for current + + ( $c{$_+1} ? $c{$_+1}*($_+1) : 0 ) ## integer + ) > $m ) && ($ = $x) for keys %c; ## if max reset max + $m ## return value } ``` -# Task 2: Merge Account -Try all combinations and -***You are given an array of accounts i.e. name with list of email addresses. Write a script to merge the accounts where possible. The accounts can only be merged if they have at least one email address in common.*** -## Observation +# Task 2: Number Collision -It is not 100% clear in the desciption - whether or not to assume the name must be the same - I am going to assume it isn't and that we chose one name from the list. +***You are given an array of integers which can move in right direction if it is positive and left direction when negative. If two numbers collide then the smaller one will explode. And if both are same then they both explode. We take the absolute value in consideration when comparing. All numbers move at the same speed, therefore any 2 numbers moving in the same direction will never collide. Write a script to find out who survives the collision.*** ## Solution -The first pass at a solution, keeps a track of which emails that we have seen and links together an account with the current one if we have already seen the email address. This works most of the time - but it can go wrong - when there are three accounts with overlapping emails BUT they have no common email address. This is the `for my $acc` loop below. To resolve this we can allow ourselves to do multiple passes reducing the list each time. +We can use a stack to achieve this. We start with an empty stack and follow these simple rules. + + 1) **IF** + 1) the list is empty + 2) the next value is +ve + 3) the last number on the stack is -ve + + **THEN** we push the next value onto the stack; + + 2) **IF** the absolute value for the top of the stack and the next value **THEN** we remove the value from the stack and throw away the current value; + + 3) **IF** the absolute value for the top of the stack is greater than the absolute value of the next value **THEN** we remove the value from we throw the current value away; -Now + 4) **Otherwise (IF)** the absolute value for the top of the stack is less than the absolute value of the next value **THEN** we remove the value from the top of the stack. ```perl -sub merge_accounts { - my($in,$out,%seen,$t) = ([],shift); - while(@{$out}!=@{$in}) { - ($in,$out,%seen) = ($out,[]); - O: for my $acc (@{$in}) { - my( $name, @e )=@{ $acc }; - for(@e) { - if( exists $seen{$_} ) { - my( $m, @f ) = @{ $out->[ $t = $seen{$_} ] }; - my %T = map { $_=>1 } @e, @f; - $seen{$_} = $t for keys %T; - $out->[ $t ] = [ $m, keys %T ]; - next O; - } - } - $seen{$_} = @{$out} for @e; - push @{$out},$acc; - } - } - $out +sub collision { + my @s; + $_[0]>0 || !@s || $s[-1] < 0 ? push @s, shift + : $s[-1] == -$_[0] ? pop @s && shift + : $s[-1] >= -$_[0] ? shift + : pop @s + while @_; + @s } ``` -- cgit From 45a2931a966fbde04251e2e183a7b70336fbb792 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 14:21:27 +0100 Subject: Update README.md --- challenge-210/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-210/james-smith/README.md b/challenge-210/james-smith/README.md index ee869efe5b..f1b4be3c65 100644 --- a/challenge-210/james-smith/README.md +++ b/challenge-210/james-smith/README.md @@ -38,7 +38,7 @@ sub kill_and_win { ( ( $x = ( $c{$_-1} ? $c{$_-1}*($_-1) : 0 ) ## Compute value + ( $c{$_ } ? $c{$_ }* $_ : 0 ) ## for current + ( $c{$_+1} ? $c{$_+1}*($_+1) : 0 ) ## integer - ) > $m ) && ($ = $x) for keys %c; ## if max reset max + ) > $m ) && ($m = $x) for keys %c; ## if max reset max $m ## return value } ``` -- cgit From ca290aed6c01cb98c4c0288d27cc93e91c58aa87 Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 27 Mar 2023 08:59:17 -0500 Subject: Update README --- challenge-210/bob-lied/README | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-210/bob-lied/README b/challenge-210/bob-lied/README index e6e0735fcb..9dcb79147f 100644 --- a/challenge-210/bob-lied/README +++ b/challenge-210/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 209 by Bob Lied +Solutions to weekly challenge 210 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-209/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-209/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-210/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-210/bob-lied -- cgit From f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Mar 2023 16:47:37 +0100 Subject: Add C++ solution --- challenge-194/paulo-custodio/cpp/ch-1.cpp | 54 ++++++++++++++++++++++++ challenge-194/paulo-custodio/cpp/ch-2.cpp | 68 +++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 challenge-194/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-194/paulo-custodio/cpp/ch-2.cpp diff --git a/challenge-194/paulo-custodio/cpp/ch-1.cpp b/challenge-194/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..f4b8a6c537 --- /dev/null +++ b/challenge-194/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,54 @@ +/* +Challenge 194 + +Task 1: Digital Clock +Submitted by: Mohammad S Anwar +You are given time in the format hh:mm with one missing digit. + +Write a script to find the highest digit between 0-9 that makes it valid time. + +Example 1 +Input: $time = '?5:00' +Output: 1 + +Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place. +Example 2 +Input: $time = '?3:00' +Output: 2 +Example 3 +Input: $time = '1?:00' +Output: 9 +Example 4 +Input: $time = '2?:00' +Output: 3 +Example 5 +Input: $time = '12:?5' +Output: 5 +Example 6 +Input: $time = '12:5?' +Output: 9 +*/ + +#include +#include + +int missing_digit(const std::string& clock) { + if (clock.size()!=5) return -1; + if (clock[0]=='?' && clock[1]<='3') return 2; + if (clock[0]=='?') return 1; + if (clock[0]<='1' && clock[1]=='?') return 9; + if (clock[1]=='?') return 3; + if (clock[3]=='?') return 5; + if (clock[4]=='?') return 9; + return -1; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc != 1) { + std::cerr << "usage: ch-1 hh:mm" << std::endl; + return EXIT_FAILURE; + } + + std::cout << missing_digit(argv[0]) << std::endl; +} diff --git a/challenge-194/paulo-custodio/cpp/ch-2.cpp b/challenge-194/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..437629244c --- /dev/null +++ b/challenge-194/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,68 @@ +/* +Challenge 194 + +Task 2: Frequency Equalizer +Submitted by: Mohammad S Anwar +You are given a string made of alphabetic characters only, a-z. + +Write a script to determine whether removing only one character can make the +frequency of the remaining characters the same. + +Example 1: +Input: $s = 'abbc' +Output: 1 since removing one alphabet 'b' will give us 'abc' where each +alphabet frequency is the same. +Example 2: +Input: $s = 'xyzyyxz' +Output: 1 since removing 'y' will give us 'xzyyxz'. +Example 3: +Input: $s = 'xzxz' +Output: 0 since removing any one alphabet would not give us string with same +frequency alphabet. +*/ + +#include +#include +#include +#include + +const int LETTERS = ('z'-'a'+1); + +int freq_equalizer(const std::string& s) { + std::vector freq; + freq.resize(LETTERS); + + if (s.empty()) return 0; + for (auto& c : s) { + if (isalpha(c)) + freq[tolower(c)-'a']++; + } + + int min=0, max=0; + for (auto& f : freq) { + if (f) { + if (min==0) min=max=f; + if (min>f) min=f; + if (max Date: Mon, 27 Mar 2023 18:53:15 +0200 Subject: Challenges 043 044 LK Perl Python --- challenge-043/lubos-kolouch/perl/ch-1.pl | 56 ++++++++++++++++++++++++++++++ challenge-043/lubos-kolouch/perl/ch-2.pl | 25 +++++++++++++ challenge-043/lubos-kolouch/python/ch-1.py | 29 ++++++++++++++++ challenge-043/lubos-kolouch/python/ch-2.py | 18 ++++++++++ challenge-044/lubos-kolouch/perl/ch-1.pl | 28 +++++++++++++++ challenge-044/lubos-kolouch/perl/ch-2.pl | 20 +++++++++++ challenge-044/lubos-kolouch/python/ch-1.py | 20 +++++++++++ challenge-044/lubos-kolouch/python/ch-2.py | 16 +++++++++ 8 files changed, 212 insertions(+) create mode 100644 challenge-043/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-043/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-043/lubos-kolouch/python/ch-1.py create mode 100644 challenge-043/lubos-kolouch/python/ch-2.py create mode 100644 challenge-044/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-044/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-044/lubos-kolouch/python/ch-1.py create mode 100644 challenge-044/lubos-kolouch/python/ch-2.py diff --git a/challenge-043/lubos-kolouch/perl/ch-1.pl b/challenge-043/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..b30d576f86 --- /dev/null +++ b/challenge-043/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,56 @@ +use strict; +use warnings; +use Algorithm::Combinatorics qw(permutations); + +# Initialize the ring numbers and available numbers +my %rings = ( + Blue => 8, + Black => undef, + Red => 9, + Yellow => 7, + Green => 5, +); +my @available = ( 1, 2, 3, 4, 6 ); + +# Generate all possible permutations of the available numbers +my $iter = permutations( \@available ); + +# Try each permutation for a valid solution +my @solutions; +while ( my $p = $iter->next ) { + my ( $b, $k, $r, $y, $g ) = @$p; + + # Skip if any number is repeated + next if ( $b == $k || $b == $r || $b == $y || $b == $g ); + next if ( $k == $r || $k == $y || $k == $g ); + next if ( $r == $y || $r == $g ); + next if ( $y == $g ); + + # Check if the sum of numbers in each ring is 11 + if ( $b + $k + $r == 11 && $r + $y + $g == 11 ) { + + # Save the solution + $rings{Blue} = $b; + $rings{Black} = $k; + $rings{Red} = $r; + $rings{Yellow} = $y; + $rings{Green} = $g; + push @solutions, {%rings}; + } +} + +# Print the solutions +if (@solutions) { + print "Found ", scalar @solutions, " solutions:\n"; + for my $i ( 0 .. $#solutions ) { + print "Solution ", $i + 1, ":\n"; + print " Blue: ", $solutions[$i]{Blue}, "\n"; + print " Black: ", $solutions[$i]{Black}, "\n"; + print " Red: ", $solutions[$i]{Red}, "\n"; + print " Yellow: ", $solutions[$i]{Yellow}, "\n"; + print " Green: ", $solutions[$i]{Green}, "\n"; + } +} +else { + print "No solutions found.\n"; +} diff --git a/challenge-043/lubos-kolouch/perl/ch-2.pl b/challenge-043/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..00ff0a69f1 --- /dev/null +++ b/challenge-043/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; + +sub self_descriptive { + my ($base) = @_; + my $n = $base; + while (1) { + if ( sd( $n, $base ) ) { + print "$n\n"; + } + $n++; + } +} + +sub sd { + my ( $n, $base ) = @_; + my @digits = split //, $n; + my @counts = (0) x $base; + for my $d (@digits) { + $counts[$d]++; + } + return join( '', @counts ) == $n; +} + +self_descriptive(10); diff --git a/challenge-043/lubos-kolouch/python/ch-1.py b/challenge-043/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..39d4aa8bb9 --- /dev/null +++ b/challenge-043/lubos-kolouch/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from itertools import permutations + +# Initialize the ring numbers and available numbers +rings = {"Blue": 8, "Black": None, "Red": 9, "Yellow": 7, "Green": 5} +available = [1, 2, 3, 4, 6] + +# Generate all possible permutations of the available numbers +for b, k, r, y, g in permutations(available): + # Skip if any number is repeated + if b == k or b == r or b == y or b == g: + continue + if k == r or k == y or k == g: + continue + if r == y or r == g: + continue + if y == g: + continue + # Check if the sum of numbers in each ring is 11 + if b + k + r == 11 and r + y + g == 11: + # Save the solution + rings["Blue"] = b + rings["Black"] = k + rings["Red"] = r + rings["Yellow"] = y + rings["Green"] = g + print(rings) diff --git a/challenge-043/lubos-kolouch/python/ch-2.py b/challenge-043/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..7faf8d6be1 --- /dev/null +++ b/challenge-043/lubos-kolouch/python/ch-2.py @@ -0,0 +1,18 @@ +def self_descriptive(base): + def sd(n): + digits = [int(d) for d in str(n)] + counts = [0] * base + for d in digits: + counts[d] += 1 + return int("".join(map(str, counts))) == n + + n = base + while True: + if sd(n): + yield n + n += 1 + + +gen = self_descriptive(10) +for _ in range(5): + print(next(gen)) diff --git a/challenge-044/lubos-kolouch/perl/ch-1.pl b/challenge-044/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f76b375086 --- /dev/null +++ b/challenge-044/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + +sub insert_operators { + my ( $str, $target ) = @_; + my @results; + helper( $str, $target, "", \@results ); + return @results; +} + +sub helper { + my ( $str, $target, $expr, $results ) = @_; + if ( $str eq "" ) { + if ( eval($expr) == $target ) { + push @$results, $expr; + } + return; + } + for my $i ( 1 .. length($str) ) { + my $left = substr( $str, 0, $i ); + my $right = substr( $str, $i ); + helper( $right, $target, "$expr+$left", $results ); + helper( $right, $target, "$expr-$left", $results ); + } +} + +my @results = insert_operators( "123456789", 100 ); +print "$_\n" for @results; diff --git a/challenge-044/lubos-kolouch/perl/ch-2.pl b/challenge-044/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..75a6d5429b --- /dev/null +++ b/challenge-044/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; + +my $goal = 200; # Target amount +my $current = 1; # Current amount +my $moves = 0; # Number of moves + +while ( $current < $goal ) { + + # Check if doubling the current amount gets us closer to the goal + if ( $current * 2 <= $goal - $current - 1 ) { + $current *= 2; + } + else { + $current += 1; + } + $moves += 1; +} + +print "To reach $goal, you need $moves moves.\n"; diff --git a/challenge-044/lubos-kolouch/python/ch-1.py b/challenge-044/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..4e58bd9a26 --- /dev/null +++ b/challenge-044/lubos-kolouch/python/ch-1.py @@ -0,0 +1,20 @@ +def insert_operators(s, target): + results = [] + + def helper(s, expr): + if not s: + if eval(expr) == target: + results.append(expr) + return + for i in range(1, len(s) + 1): + left, right = s[:i], s[i:] + helper(right, expr + "+" + left) + helper(right, expr + "-" + left) + + helper(s, "") + return results + + +results = insert_operators("123456789", 100) +for r in results: + print(r) diff --git a/challenge-044/lubos-kolouch/python/ch-2.py b/challenge-044/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4ad8efde17 --- /dev/null +++ b/challenge-044/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +goal = 200 # Target amount +current = 1 # Current amount +moves = 0 # Number of moves + +while current < goal: + # Check if doubling the current amount gets us closer to the goal + if current * 2 <= goal - current - 1: + current *= 2 + else: + current += 1 + moves += 1 + +print("To reach $", goal, ", you need", moves, "moves.") -- cgit From 5131272f93bab607256e10d1434dedf1c6941904 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:58:35 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 33 ++++++++++++++++++++++++++++ challenge-210/mark-anderson/raku/ch-2.raku | 35 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-210/mark-anderson/raku/ch-1.raku create mode 100644 challenge-210/mark-anderson/raku/ch-2.raku diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..7b60d4b866 --- /dev/null +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use Test; + +is kill-and-win(2,3,1), 6; # choosing 2 +is kill-and-win(1,1,2,2,2,3), 11; # choosing 2 +is kill-and-win(1,1,3,3,5,5), 10; # choosing 5 +is kill-and-win(4,4,4,4,7,8), 16; # choosing 4 +is kill-and-win(4,4,4,4,7,7,8), 22; # choosing 7 +is kill-and-win(8,6,3,3,8,1,9,3,1,4,5,2,5,4,5,4,7,3,3,5), 47; # choosing 4 +is kill-and-win(2,6,1,1,6,5,6,3,4,9,8,3,3,2,3,2,3,3,7,9), 33; # choosing 7 or 8 +is kill-and-win(6,4,5,4,1,3,2,9,2,4,7,1,1,9,8,2,2,2,4,4), 33; # choosing 3 or 8 + +sub kill-and-win(*@ints) +{ + my @a = (sort .keys Z=> (.keys Z* .values) given @ints.Bag) + .Array + .push(@ints.max.succ => 0) + .unshift(@ints.min.pred => 0) + .rotor(3 => -2); + + @a.map(&total).max +} + +sub total(@a) +{ + my @slice = .[0] eq .[1]-1 eq .[2]-2 ?? (0,1,2) !! + .[0] eq .[1]-1 ?? (0,1) !! + .[1] eq .[2]-1 ?? (1,2) !! + (1) + given @a.Map.keys.sort(+*); + + [+] @a[@slice].Map.values +} diff --git a/challenge-210/mark-anderson/raku/ch-2.raku b/challenge-210/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..5a8fcdb255 --- /dev/null +++ b/challenge-210/mark-anderson/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use Test; + +is-deeply number-collisions(-2,-1,-3,1,2,3), + (-2,-1,-3,1,2,3); + +is-deeply number-collisions(1,2,3,-3,-1), + (1,2); + +is-deeply number-collisions(-2,4,5,-3,-1,9,-8), + (-2,4,5,9); + +is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), + (-76,-8,-19,-99); + +is-deeply number-collisions(99,12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), + (); + +is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0), + (-76,-8,-19,11,2,0); + +sub number-collisions(*@a) +{ + given $_ = @a.join(' ') + { + while / (\d+) \s (\-\d+) / + { + when $0 < $1.abs { .substr-rw($0.from, $0.to.succ - $0.from) = Empty } + when $0 > $1.abs { .substr-rw($1.from, $1.to.succ - $1.from) = Empty } + default { .substr-rw($0.from, $1.to.succ - $0.from) = Empty } + } + + .comb(/ \-? \d+ /)>>.Int + } +} -- cgit From 3c83d2fb8db2427385006d01fa90f6d732ebf980 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 27 Mar 2023 14:25:40 -0400 Subject: Week 210 --- challenge-210/zapwai/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-210/zapwai/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 challenge-210/zapwai/perl/ch-1.pl create mode 100644 challenge-210/zapwai/perl/ch-2.pl diff --git a/challenge-210/zapwai/perl/ch-1.pl b/challenge-210/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..1f03462196 --- /dev/null +++ b/challenge-210/zapwai/perl/ch-1.pl @@ -0,0 +1,20 @@ +use v5.30.0; +my @int = (2, 3, 1); +#my @int = (1,1,2,2, 3, 2); +say "Input: \@int = (".join(", ",@int).")"; +my %freq; +$freq{$_}++ for (@int); +my $max; +my $val; +foreach my $key (keys %freq) { + my $sum = 0; + for (-1 .. 1) { + my $n = $key + $_; + $sum += $n*$freq{$n}; + } + if ($max < $sum) { + $val = $key; + $max = $sum; + } +} +say "Output: $max (by removing $val)"; diff --git a/challenge-210/zapwai/perl/ch-2.pl b/challenge-210/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..f43a9e9234 --- /dev/null +++ b/challenge-210/zapwai/perl/ch-2.pl @@ -0,0 +1,25 @@ +use v5.30.0; +my @list = (2,3,-1); +say "Input: \@list = (".join(", ",@list).")"; +for (1 .. @list) { + my $positive_value_preceding = 0; + last if ($#list == 0); + foreach my $i (0 .. $#list) { + if ($list[$i] < 0) { + if ($positive_value_preceding) { + my $bad_entry = $i; + $bad_entry-- if ( abs($list[$i - 1]) <= abs($list[$i]) ); + if (abs($list[$i - 1]) == abs($list[$i])) { + splice @list, $i, 1; + $i--; + } + splice @list, $bad_entry, 1; + $positive_value_preceding = 0; + redo; + } + } else { + $positive_value_preceding = 1; + } + } +} +say "Output: (".join(", ",@list).")"; -- cgit From bbd881b07636d6d00920ad5a745807d59b85ef26 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:49:19 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 13 ++++++------- challenge-210/mark-anderson/raku/ch-2.raku | 25 ++++++++----------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku index 7b60d4b866..fb41312ce6 100644 --- a/challenge-210/mark-anderson/raku/ch-1.raku +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -12,13 +12,12 @@ is kill-and-win(6,4,5,4,1,3,2,9,2,4,7,1,1,9,8,2,2,2,4,4), 33; # choosing 3 or 8 sub kill-and-win(*@ints) { - my @a = (sort .keys Z=> (.keys Z* .values) given @ints.Bag) - .Array - .push(@ints.max.succ => 0) - .unshift(@ints.min.pred => 0) - .rotor(3 => -2); - - @a.map(&total).max + (sort .keys Z=> (.keys Z* .values) given @ints.Bag) + .Array + .push(@ints.max.succ => 0) + .unshift(@ints.min.pred => 0) + .rotor(3 => -2) + .map(&total).max } sub total(@a) diff --git a/challenge-210/mark-anderson/raku/ch-2.raku b/challenge-210/mark-anderson/raku/ch-2.raku index 5a8fcdb255..ada0b9ed31 100644 --- a/challenge-210/mark-anderson/raku/ch-2.raku +++ b/challenge-210/mark-anderson/raku/ch-2.raku @@ -1,23 +1,14 @@ #!/usr/bin/env raku use Test; -is-deeply number-collisions(-2,-1,-3,1,2,3), - (-2,-1,-3,1,2,3); - -is-deeply number-collisions(1,2,3,-3,-1), - (1,2); - -is-deeply number-collisions(-2,4,5,-3,-1,9,-8), - (-2,4,5,9); - -is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), - (-76,-8,-19,-99); - -is-deeply number-collisions(99,12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), - (); - -is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0), - (-76,-8,-19,11,2,0); +is-deeply number-collisions(2,3,-1), (2,3); +is-deeply number-collisions(3,2,-4), (-4,); +is-deeply number-collisions(-2,-1,-3,1,2,3), (-2,-1,-3,1,2,3); +is-deeply number-collisions(1,2,3,-3,-1), (1,2); +is-deeply number-collisions(-2,4,5,-3,-1,9,-8), (-2,4,5,9); +is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), (-76,-8,-19,-99); +is-deeply number-collisions(99,12,43,-76,-8,88,-88,-19,11,2,-1,0,-99), (); +is-deeply number-collisions(12,43,-76,-8,88,-88,-19,11,2,-1,0), (-76,-8,-19,11,2,0); sub number-collisions(*@a) { -- cgit From d7023365540262ba297d70f52bff97caea7a97c5 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 27 Mar 2023 22:44:35 +0200 Subject: Add solutions to 210: Kill and Win & Number Collision by E. Choroba --- challenge-210/e-choroba/perl/ch-1.pl | 14 ++++++++++++ challenge-210/e-choroba/perl/ch-2.pl | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 challenge-210/e-choroba/perl/ch-1.pl create mode 100755 challenge-210/e-choroba/perl/ch-2.pl diff --git a/challenge-210/e-choroba/perl/ch-1.pl b/challenge-210/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4603fed9c1 --- /dev/null +++ b/challenge-210/e-choroba/perl/ch-1.pl @@ -0,0 +1,14 @@ +#! /usr/bin/perl +use warnings; +use strict; + +# In the end, all the numbers must be exhausted (easy to prove by +# contradiction), so their total is just the sum of all the numbers. + +use Importer 'List::Util' => + sum => {-as => 'kill_and_win'}; + +use Test::More tests => 2; + +is kill_and_win(2, 3, 1), 6, 'Example 1'; +is kill_and_win(1, 1, 2, 2, 2, 3), 11, 'Example 2'; diff --git a/challenge-210/e-choroba/perl/ch-2.pl b/challenge-210/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..766b505064 --- /dev/null +++ b/challenge-210/e-choroba/perl/ch-2.pl @@ -0,0 +1,41 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +# What happens to zeroes? They don't move, but I interpret the rules +# in the following way: if any number tries to move into a zero, the +# zero explodes (see the additional tests). + +sub number_collision(@list) { + while (1) { + my $changed; + my @new = @list; + for (my $i = 0; $i <= $#list; ++$i) { + if ($list[$i] > 0 && $i < $#list && $list[ $i + 1 ] <= 0) { + @new[$i, $i + 1] = ( + undef, + (undef, @list[$i, $i + 1])[ + abs $list[$i] <=> abs $list[ $i + 1 ]]); + ++$i; + $changed = 1; + } elsif ($list[$i] == 0 && $i < $#list && $list[ $i + 1 ] < 0) { + @new[$i, $i + 1] = (undef, $list[ $i + 1 ]); + } + } + @list = grep defined, @new; + last unless $changed + } + return \@list +} + +use Test2::V0; +plan 3 + 3; + +is number_collision(2, 3, -1), [2, 3], 'Example 1'; +is number_collision(3, 2, -4), [-4], 'Example 2'; +is number_collision(1, -1), [], 'Example 3'; + +is number_collision(0, 0), [0, 0], 'Only zeroes'; +is number_collision(1, 0), [1], "Zeroes don't move positive"; +is number_collision(0, -1), [-1], "Zeroes don't move negative"; -- cgit From 236529b48e11f0d4bc8f912db5d21482eafa7a78 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Mar 2023 22:13:45 +0100 Subject: Update README.md --- challenge-210/james-smith/README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/challenge-210/james-smith/README.md b/challenge-210/james-smith/README.md index f1b4be3c65..2888dd9d66 100644 --- a/challenge-210/james-smith/README.md +++ b/challenge-210/james-smith/README.md @@ -61,17 +61,19 @@ We can use a stack to achieve this. We start with an empty stack and follow thes 2) **IF** the absolute value for the top of the stack and the next value **THEN** we remove the value from the stack and throw away the current value; - 3) **IF** the absolute value for the top of the stack is greater than the absolute value of the next value **THEN** we remove the value from we throw the current value away; + 3) **IF** the absolute value for the top of the stack is less than the absolute value of the next value **THEN** we remove the value from the top of the stack; - 4) **Otherwise (IF)** the absolute value for the top of the stack is less than the absolute value of the next value **THEN** we remove the value from the top of the stack. + 4) **Otherwise (IF)** the absolute value for the top of the stack is greater than the absolute value of the next value **THEN** we just throw the current value away. ```perl sub collision { my @s; - $_[0]>0 || !@s || $s[-1] < 0 ? push @s, shift - : $s[-1] == -$_[0] ? pop @s && shift - : $s[-1] >= -$_[0] ? shift - : pop @s + !@s || + $_[0] > 0 || + 0 > $s[-1] ? push @s, shift + : -$_[0] == $s[-1] ? pop @s && shift + : -$_[0] >= $s[-1] ? pop @s + : shift while @_; @s } -- cgit From 1f0e0a061b0e758d9a10010c573443f1dcf9ae20 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Mar 2023 22:51:55 +0100 Subject: Add Perl solution --- challenge-193/paulo-custodio/Makefile | 2 + challenge-193/paulo-custodio/perl/ch-1.pl | 23 +++++++++ challenge-193/paulo-custodio/perl/ch-2.pl | 78 ++++++++++++++++++++++++++++++ challenge-193/paulo-custodio/t/test-1.yaml | 10 ++++ challenge-193/paulo-custodio/t/test-2.yaml | 15 ++++++ 5 files changed, 128 insertions(+) create mode 100644 challenge-193/paulo-custodio/Makefile create mode 100644 challenge-193/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-193/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-193/paulo-custodio/t/test-1.yaml create mode 100644 challenge-193/paulo-custodio/t/test-2.yaml diff --git a/challenge-193/paulo-custodio/Makefile b/challenge-193/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-193/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-193/paulo-custodio/perl/ch-1.pl b/challenge-193/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..4f0c936287 --- /dev/null +++ b/challenge-193/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# Challenge 193 +# +# Task 1: Binary String +# Submitted by: Mohammad S Anwar +# You are given an integer, $n > 0. +# +# Write a script to find all possible binary numbers of size $n. +# +# Example 1 +# Input: $n = 2 +# Output: 00, 11, 01, 10 +# Example 2 +# Input: $n = 3 +# Output: 000, 001, 010, 100, 111, 110, 101, 011 + +use Modern::Perl; + +@ARGV==1 or die "usage: ch-1.pl n\n"; +my $n=shift||1; +say join ", ", map{sprintf("%0${n}b", $_)} (0..2**$n-1); + diff --git a/challenge-193/paulo-custodio/perl/ch-2.pl b/challenge-193/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8a3d5f6484 --- /dev/null +++ b/challenge-193/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl + +# Challenge 193 +# +# Task 2: Odd String +# Submitted by: Mohammad S Anwar +# You are given a list of strings of same length, @s. +# +# Write a script to find the odd string in the given list. Use positional value +# of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25. +# +# Find the difference array for each string as shown in the example. Then pick +# the odd one out. +# +# Example 1: +# Input: @s = ("adc", "wzy", "abc") +# Output: "abc" +# +# Difference array for "adc" => [ d - a, c - d ] +# => [ 3 - 0, 2 - 3 ] +# => [ 3, -1 ] +# +# Difference array for "wzy" => [ z - w, y - z ] +# => [ 25 - 22, 24 - 25 ] +# => [ 3, -1 ] +# +# Difference array for "abc" => [ b - a, c - b ] +# => [ 1 - 0, 2 - 1 ] +# => [ 1, 1 ] +# +# The difference array for "abc" is the odd one. +# Example 2: +# Input: @s = ("aaa", "bob", "ccc", "ddd") +# Output: "bob" +# +# Difference array for "aaa" => [ a - a, a - a ] +# => [ 0 - 0, 0 - 0 ] +# => [ 0, 0 ] +# +# Difference array for "bob" => [ o - b, b - o ] +# => [ 14 - 1, 1 - 14 ] +# => [ 13, -13 ] +# +# Difference array for "ccc" => [ c - c, c - c ] +# => [ 2 - 2, 2 - 2 ] +# => [ 0, 0 ] +# +# Difference array for "ddd" => [ d - d, d - d ] +# => [ 3 - 3, 3 - 3 ] +# => [ 0, 0 ] +# +# The difference array for "bob" is the odd one. + +use Modern::Perl; + +sub string_diff { + my($string)=@_; + my @values = map {ord} split //, $string; + my @diff = map {$values[$_+1]-$values[$_]} 0..$#values-1; + return @diff; +} + +sub odd_string { + my(@strings)=@_; + my %diffs; + my %strings; + for (@strings) { + my @diffs = string_diff($_); + $diffs{"@diffs"}++; + $strings{"@diffs"}=$_; + } + my @odd = grep {$diffs{$_}==1} keys %diffs; + return @odd==1 ? $strings{$odd[0]} : "."; +} + +@ARGV or die "usage: ch-2.pl string...\n"; +my @strings=@ARGV; +say odd_string(@strings); diff --git a/challenge-193/paulo-custodio/t/test-1.yaml b/challenge-193/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f23806ddcd --- /dev/null +++ b/challenge-193/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 + input: + output: 00, 01, 10, 11 +- setup: + cleanup: + args: 3 + input: + output: 000, 001, 010, 011, 100, 101, 110, 111 diff --git a/challenge-193/paulo-custodio/t/test-2.yaml b/challenge-193/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1b1854b292 --- /dev/null +++ b/challenge-193/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: adc wzy abc + input: + output: abc +- setup: + cleanup: + args: aaa bob ccc ddd + input: + output: bob +- setup: + cleanup: + args: aaa ccc ddd + input: + output: . -- cgit From 4749a7e6fc6b4f0a21a582742e195fda3f41e7bb Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 27 Mar 2023 18:16:02 -0600 Subject: SOlve PWC210 --- challenge-210/wlmb/blog.txt | 2 ++ challenge-210/wlmb/perl/ch-1.pl | 12 ++++++++++++ challenge-210/wlmb/perl/ch-2.pl | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 challenge-210/wlmb/blog.txt create mode 100755 challenge-210/wlmb/perl/ch-1.pl create mode 100755 challenge-210/wlmb/perl/ch-2.pl diff --git a/challenge-210/wlmb/blog.txt b/challenge-210/wlmb/blog.txt new file mode 100644 index 0000000000..674cd2d271 --- /dev/null +++ b/challenge-210/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/03/27/PWC210/ + diff --git a/challenge-210/wlmb/perl/ch-1.pl b/challenge-210/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..94e35f4ba7 --- /dev/null +++ b/challenge-210/wlmb/perl/ch-1.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +# Perl weekly challenge 210 +# Task 1: Kill and Win +# +# See https://wlmb.github.io/2023/03/27/PWC210/#task-1-kill-and-win +use v5.36; +use List::Util qw(sum); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to get the maximum points when killing the numbers N1 N2... + FIN +say "(@ARGV) -> ", sum @ARGV; diff --git a/challenge-210/wlmb/perl/ch-2.pl b/challenge-210/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..d7d00b1a73 --- /dev/null +++ b/challenge-210/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 210 +# Task 2: Number Collision +# +# See https://wlmb.github.io/2023/03/27/PWC210/#task-2-number-collision +use v5.36; +use List::Util qw(first); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + To find which numbers among N1 N2... survive all collisions + FIN +my @numbers=@ARGV; # copy input +while(defined( # search for index of next collision + my $collision=first { + $numbers[$_]>$numbers[$_+1] && $numbers[$_]>=0>=$numbers[$_+1] + } + 0..@numbers-2 + ) + ){ + splice @numbers, $collision + 1, 1 if $numbers[$collision] > -$numbers[$collision+1]; + splice @numbers, $collision, 1 if $numbers[$collision] < -$numbers[$collision+1]; + splice @numbers, $collision, 2 if $numbers[$collision] == -$numbers[$collision+1]; +} +say "(@ARGV) -> (@numbers)" -- cgit From 39f196f77e492f8bfafcf1b5c23eea2c28040de7 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Tue, 28 Mar 2023 07:01:29 +0200 Subject: Remove bug noted by github/oldtechaa --- challenge-209/polettix/perl/ch-2.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-209/polettix/perl/ch-2.pl b/challenge-209/polettix/perl/ch-2.pl index b6c7fe78e4..8ed759586b 100644 --- a/challenge-209/polettix/perl/ch-2.pl +++ b/challenge-209/polettix/perl/ch-2.pl @@ -68,8 +68,8 @@ sub merge_accounts ($aref) { if (defined($last_wiped)) { my $marker = my $cursor = $last_wiped; while (++$cursor < $all_groups->$#*) { - next if defined($all_groups->[$cursor]); - $all_groups->[$marker++] = $all_groups->[$cursor]; + $all_groups->[$marker++] = $all_groups->[$cursor] + if defined($all_groups->[$cursor]); } splice $all_groups->@*, $marker if $marker < $all_groups->@*; } -- cgit From d2082fb7b484a4506fa3d5c7e1ae7596838bf965 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 28 Mar 2023 07:47:41 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku index fb41312ce6..8741bef4a2 100644 --- a/challenge-210/mark-anderson/raku/ch-1.raku +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -12,12 +12,14 @@ is kill-and-win(6,4,5,4,1,3,2,9,2,4,7,1,1,9,8,2,2,2,4,4), 33; # choosing 3 or 8 sub kill-and-win(*@ints) { - (sort .keys Z=> (.keys Z* .values) given @ints.Bag) - .Array - .push(@ints.max.succ => 0) - .unshift(@ints.min.pred => 0) - .rotor(3 => -2) - .map(&total).max + @ints.Bag + .Array + .push(@ints.max.succ => 0) + .unshift(@ints.min.pred => 0) + .sort + .rotor(3 => -2) + .map(&total) + .max } sub total(@a) @@ -28,5 +30,5 @@ sub total(@a) (1) given @a.Map.keys.sort(+*); - [+] @a[@slice].Map.values + [+] .keys Z* .values given @a[@slice].Map } -- cgit From 88ac5a416468ac3dc894669ce4535e337fbf2764 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:21:45 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku index 8741bef4a2..ce676caa8e 100644 --- a/challenge-210/mark-anderson/raku/ch-1.raku +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -24,9 +24,9 @@ sub kill-and-win(*@ints) sub total(@a) { - my @slice = .[0] eq .[1]-1 eq .[2]-2 ?? (0,1,2) !! - .[0] eq .[1]-1 ?? (0,1) !! - .[1] eq .[2]-1 ?? (1,2) !! + my @slice = .[0] == .[1]-1 == .[2]-2 ?? (0,1,2) !! + .[0] == .[1]-1 ?? (0,1) !! + .[1] == .[2]-1 ?? (1,2) !! (1) given @a.Map.keys.sort(+*); -- cgit From e8aa7d95e068054b568f60a5828f1a17d54f751e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:29:52 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku index ce676caa8e..911c1f540e 100644 --- a/challenge-210/mark-anderson/raku/ch-1.raku +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -13,10 +13,10 @@ is kill-and-win(6,4,5,4,1,3,2,9,2,4,7,1,1,9,8,2,2,2,4,4), 33; # choosing 3 or 8 sub kill-and-win(*@ints) { @ints.Bag + .sort .Array .push(@ints.max.succ => 0) .unshift(@ints.min.pred => 0) - .sort .rotor(3 => -2) .map(&total) .max -- cgit From c62ba86c52ad00d866516834828dd7731c63d551 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 28 Mar 2023 09:43:56 +0100 Subject: Add Perl solution --- challenge-210/paulo-custodio/Makefile | 2 + challenge-210/paulo-custodio/perl/ch-1.pl | 52 +++++++++++++++++++++ challenge-210/paulo-custodio/perl/ch-2.pl | 74 ++++++++++++++++++++++++++++++ challenge-210/paulo-custodio/t/test-1.yaml | 10 ++++ challenge-210/paulo-custodio/t/test-2.yaml | 15 ++++++ 5 files changed, 153 insertions(+) create mode 100644 challenge-210/paulo-custodio/Makefile create mode 100644 challenge-210/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-210/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-210/paulo-custodio/t/test-1.yaml create mode 100644 challenge-210/paulo-custodio/t/test-2.yaml diff --git a/challenge-210/paulo-custodio/Makefile b/challenge-210/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-210/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-210/paulo-custodio/perl/ch-1.pl b/challenge-210/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d7ff8dc143 --- /dev/null +++ b/challenge-210/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# Challenge 210 +# +# Task 1: Kill and Win +# Submitted by: Mohammad S Anwar +# +# You are given a list of integers. +# +# Write a script to get the maximum points. You are allowed to take out (kill) +# any integer and remove from the list. However if you do that then all +# integers exactly one-less or one-more would also be removed. Find out the +# total of integers removed. +# Example 1 +# +# Input: @int = (2, 3, 1) +# Output: 6 +# +# First we delete 2 and that would also delete 1 and 3. So the maximum points +# we get is 6. +# +# Example 2 +# +# Input: @int = (1, 1, 2, 2, 2, 3) +# Output: 11 +# +# First we delete 2 and that would also delete both the 1's and the 3. Now we +# have (2, 2). +# Then we delete another 2 and followed by the third deletion of 2. So the +# maximum points we get is 11. + +use Modern::Perl; +use List::Util 'sum'; + +sub kill_and_win { + my(@n)=@_; + return 0 if @n==0; + # kill one and recurse + my $max=0; + for my $i (0..$#n) { + my $kill=$n[$i]; + my @copy=@n; + splice(@copy,$i,1); # remove kill value + my $value=$kill+sum(0,grep {$_==$kill+1 || $_==$kill-1} @copy); + @copy=grep {$_!=$kill+1 && $_!=$kill-1} @copy; # remove above and below + $value+=kill_and_win(@copy); + $max=$value if $max<$value; + } + return $max; +} + +say kill_and_win(@ARGV); diff --git a/challenge-210/paulo-custodio/perl/ch-2.pl b/challenge-210/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b9b98eeb6f --- /dev/null +++ b/challenge-210/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +# Challenge 210 +# +# Task 2: Number Collision +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers which can move in right direction if it is +# positive and left direction when negative. If two numbers collide then the +# smaller one will explode. And if both are same then they both explode. We +# take the absolute value in consideration when comparing. +# +# All numbers move at the same speed, therefore any 2 numbers moving in the same +# direction will never collide. +# +# Write a script to find out who survives the collision. +# Example 1: +# +# Input: @list = (2, 3, -1) +# Output: (2, 3) +# +# The numbers 3 and -1 collide and -1 explodes in the end. So we are left +# with (2, 3). +# +# Example 2: +# +# Input: @list = (3, 2, -4) +# Output: (-4) +# +# The numbers 2 and -4 collide and 2 explodes in the end. That gives us (3, -4). +# Now the numbers 3 and -4 collide and 3 explodes. Finally we are left with -4. +# +# Example 3: +# +# Input: @list = (1, -1) +# Output: () +# +# The numbers 1 and -1 both collide and explode. Nothing left in the end. + +use Modern::Perl; + +sub find_collision { + my(@n)=@_; + for my $i (0..$#n-1) { + if ($n[$i]>0 && $n[$i+1]<0 || + $n[$i]<0 && $n[$i+1]>0) { + return $i; + } + } + return -1; +} + +sub number_collision { + my(@n)=@_; + my $i; + while (($i=find_collision(@n))>=0) { + if (abs($n[$i])==abs($n[$i+1])) { + splice(@n,$i,2); # both explode + } + elsif (abs($n[$i])>abs($n[$i+1])) { + splice(@n,$i+1,1); # right explodes + } + elsif (abs($n[$i]) Date: Tue, 28 Mar 2023 09:14:27 +0000 Subject: Challenge 210 Solutions (Raku) --- challenge-210/mark-anderson/raku/ch-1.raku | 2 ++ challenge-210/mark-anderson/raku/ch-2.raku | 2 ++ 2 files changed, 4 insertions(+) diff --git a/challenge-210/mark-anderson/raku/ch-1.raku b/challenge-210/mark-anderson/raku/ch-1.raku index 911c1f540e..4b229c0fb2 100644 --- a/challenge-210/mark-anderson/raku/ch-1.raku +++ b/challenge-210/mark-anderson/raku/ch-1.raku @@ -1,6 +1,8 @@ #!/usr/bin/env raku use Test; +say kill-and-win((1..100).roll(1_000_000)); # takes about 5 seconds + is kill-and-win(2,3,1), 6; # choosing 2 is kill-and-win(1,1,2,2,2,3), 11; # choosing 2 is kill-and-win(1,1,3,3,5,5), 10; # choosing 5 diff --git a/challenge-210/mark-anderson/raku/ch-2.raku b/challenge-210/mark-anderson/raku/ch-2.raku index ada0b9ed31..8a150dcd56 100644 --- a/challenge-210/mark-anderson/raku/ch-2.raku +++ b/challenge-210/mark-anderson/raku/ch-2.raku @@ -1,6 +1,8 @@ #!/usr/bin/env raku use Test; +# I'm treating 0 as a positive number + is-deeply number-collisions(2,3,-1), (2,3); is-deeply number-collisions(3,2,-4), (-4,); is-deeply number-collisions(-2,-1,-3,1,2,3), (-2,-1,-3,1,2,3); -- cgit From 67729b2d0fe4d89c2af4a571eb65580972fd8f03 Mon Sep 17 00:00:00 2001 From: dcw Date: Tue, 28 Mar 2023 15:24:13 +0100 Subject: belatedly added my C implementation of task 2, really quite tricky - had to build a simple set module (slist.[ch]) --- challenge-209/duncan-c-white/C/.cbuild | 1 - challenge-209/duncan-c-white/C/Makefile | 11 +- challenge-209/duncan-c-white/C/README | 18 +- challenge-209/duncan-c-white/C/ch-2.c | 273 ++++++++++++++++++++++++++++ challenge-209/duncan-c-white/C/csvsplit.c | 46 +++-- challenge-209/duncan-c-white/C/csvsplit.h | 13 +- challenge-209/duncan-c-white/C/parseints.c | 114 ------------ challenge-209/duncan-c-white/C/parseints.h | 1 - challenge-209/duncan-c-white/C/printarray.c | 39 ---- challenge-209/duncan-c-white/C/printarray.h | 1 - challenge-209/duncan-c-white/C/slist.c | 148 +++++++++++++++ challenge-209/duncan-c-white/C/slist.h | 25 +++ challenge-209/duncan-c-white/README | 4 +- challenge-209/duncan-c-white/perl/ch-2.pl | 7 +- 14 files changed, 507 insertions(+), 194 deletions(-) create mode 100644 challenge-209/duncan-c-white/C/ch-2.c delete mode 100644 challenge-209/duncan-c-white/C/parseints.c delete mode 100644 challenge-209/duncan-c-white/C/parseints.h delete mode 100644 challenge-209/duncan-c-white/C