From e7695a05273901cd48ec10406960cd11b923356b Mon Sep 17 00:00:00 2001 From: dcw Date: Tue, 4 Apr 2023 10:15:35 +0100 Subject: imported (belatedly) my solutions to challenge 210, in Perl and C as usual.. --- challenge-210/duncan-c-white/C/.cbuild | 1 - challenge-210/duncan-c-white/C/Makefile | 18 +++ challenge-210/duncan-c-white/C/README | 9 ++ challenge-210/duncan-c-white/C/args.c | 234 ++++++++++++++++++++++++++++ challenge-210/duncan-c-white/C/args.h | 12 ++ challenge-210/duncan-c-white/C/ch-1.c | 158 +++++++++++++++++++ challenge-210/duncan-c-white/C/ch-2.c | 148 ++++++++++++++++++ challenge-210/duncan-c-white/C/parseints.c | 114 ++++++++++++++ challenge-210/duncan-c-white/C/parseints.h | 1 + challenge-210/duncan-c-white/C/printarray.c | 39 +++++ challenge-210/duncan-c-white/C/printarray.h | 1 + challenge-210/duncan-c-white/README | 111 +++++++------ challenge-210/duncan-c-white/perl/ch-1.pl | 93 +++++++++++ challenge-210/duncan-c-white/perl/ch-2.pl | 121 ++++++++++++++ 14 files changed, 1013 insertions(+), 47 deletions(-) create mode 100644 challenge-210/duncan-c-white/C/Makefile create mode 100644 challenge-210/duncan-c-white/C/README create mode 100644 challenge-210/duncan-c-white/C/args.c create mode 100644 challenge-210/duncan-c-white/C/args.h create mode 100644 challenge-210/duncan-c-white/C/ch-1.c create mode 100644 challenge-210/duncan-c-white/C/ch-2.c create mode 100644 challenge-210/duncan-c-white/C/parseints.c create mode 100644 challenge-210/duncan-c-white/C/parseints.h create mode 100644 challenge-210/duncan-c-white/C/printarray.c create mode 100644 challenge-210/duncan-c-white/C/printarray.h create mode 100755 challenge-210/duncan-c-white/perl/ch-1.pl create mode 100755 challenge-210/duncan-c-white/perl/ch-2.pl diff --git a/challenge-210/duncan-c-white/C/.cbuild b/challenge-210/duncan-c-white/C/.cbuild index 835981f6f1..a14ec76520 100644 --- a/challenge-210/duncan-c-white/C/.cbuild +++ b/challenge-210/duncan-c-white/C/.cbuild @@ -1,5 +1,4 @@ BUILD = ch-1 ch-2 -BUILD = ch-1 CFLAGS = -Wall -g #LDFLAGS = -lm #CFLAGS = -g diff --git a/challenge-210/duncan-c-white/C/Makefile b/challenge-210/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..1b34ccd3b2 --- /dev/null +++ b/challenge-210/duncan-c-white/C/Makefile @@ -0,0 +1,18 @@ +# Makefile rules generated by CB +CC = gcc +CFLAGS = -Wall -g +BUILD = ch-1 ch-2 + +all: $(BUILD) + +clean: + /bin/rm -f $(BUILD) *.o core a.out + +args.o: args.c +ch-1: ch-1.o args.o parseints.o printarray.o +ch-1.o: ch-1.c args.h parseints.h printarray.h +ch-2: ch-2.o args.o parseints.o printarray.o +ch-2.o: ch-2.c args.h parseints.h printarray.h +parseints.o: parseints.c args.h parseints.h printarray.h +printarray.o: printarray.c + diff --git a/challenge-210/duncan-c-white/C/README b/challenge-210/duncan-c-white/C/README new file mode 100644 index 0000000000..bb9a44abc0 --- /dev/null +++ b/challenge-210/duncan-c-white/C/README @@ -0,0 +1,9 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2.pl into C.. + +Both C versions produce near identical (non-debugging and debugging) +output to the Perl originals. + +These C versions use some of my regular support modules: +- a tweaked version of my command-line argument processing module args.[ch], +- a tweaked version of csvlist-of-int parsing module parseints.[ch], and +- an int-array printing module printarray.[ch]. diff --git a/challenge-210/duncan-c-white/C/args.c b/challenge-210/duncan-c-white/C/args.c new file mode 100644 index 0000000000..20c21e6c30 --- /dev/null +++ b/challenge-210/duncan-c-white/C/args.c @@ -0,0 +1,234 @@ +#include +#include +#include +#include +#include +#include + + +bool debug = false; + + +// process_flag_noarg( name, argc, argv ); +// Process the -d flag, and check that there are no +// remaining arguments. +void process_flag_noarg( char *name, int argc, char **argv ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != 0 ) + { + fprintf( stderr, "Usage: %s [-d]\n", name ); + exit(1); + } +} + + +// int argno = process_flag_n_args( name, argc, argv, n, argmsg ); +// Process the -d flag, and check that there are exactly +// n remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != n ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d " + "arguments needed\n", name, argmsg, n ); + exit(1); + } + return arg; +} + + +// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg ); +// Process the -d flag, and check that there are between +// min and max remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < min || left > max ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d " + "arguments needed\n", name, argmsg, min, max ); + exit(1); + } + return arg; +} + + +// process_onenumarg_default( name, argc, argv, defvalue, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument (or no arguments, in which case +// we use the defvalue), putting it into n +void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n ) +{ + char argmsg[100]; + sprintf( argmsg, "[int default %d]", defvalue ); + int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg ); + + *n = arg == argc ? defvalue : atoi( argv[arg] ); +} + + +// process_onenumarg( name, argc, argv, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument, putting it into n +void process_onenumarg( char *name, int argc, char **argv, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 1, "int" ); + + // argument is in argv[arg] + *n = atoi( argv[arg] ); +} + + +// process_twonumargs( name, argc, argv, &m, &n ); +// Process the -d flag, and check that there are 2 +// remaining numeric arguments, putting them into m and n +void process_twonumargs( char *name, int argc, char **argv, int *m, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 2, "int" ); + + // arguments are in argv[arg] and argv[arg+1] + *m = atoi( argv[arg++] ); + *n = atoi( argv[arg] ); +} + + +// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead + + +// int arr[100]; +// int nel = process_listnumargs( name, argc, argv, arr, 100 ); +// Process the -d flag, and check that there are >= 2 +// remaining numeric arguments, putting them into arr[0..nel-1] +// and returning nel. +int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < 2 ) + { + fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name ); + exit(1); + } + if( left > maxel ) + { + fprintf( stderr, "%s: more than %d args\n", name, maxel ); + exit(1); + } + + // elements are in argv[arg], argv[arg+1]... + + if( debug ) + { + printf( "debug: remaining arguments are in arg=%d, " + "firstn=%s, secondn=%s..\n", + arg, argv[arg], argv[arg+1] ); + } + + int nel = 0; + for( int i=arg; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +#define MAX(a,b) ((a)>(b)?(a):(b)) + +int max = 0; + +int intcompare( const void *a, const void *b ) +{ + return *((int *)a) - *((int *)b); +} + + +// +// int pos = posinlist( v, nel, list ); +// Return the position of v if it's in list[nel], -1 otherwise +// +int posinlist( int v, int nel, int *list ) +{ + for( int i=0; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +// +// int pos = find_pos_neg( nel, list ); +// Search in the given list[nel] for a positive number +// immediately followed by a negative number. +// If you find such a pair return the position of +// the positive number. +// If there is no such pair, return -1. +// +int find_pos_neg( int nel, int *list ) +{ + for( int i=0; i=0 && list[i+1]<0 ) + { + return i; + } + } + return -1; +} + + +// +// int newnel = remove_elements( nel, list, delpos, ndel ); +// Delete ndel elements starting at delpos from list[nel], +// returning the new number of elements (should be nel-ndel) +// +int remove_elements( int nel, int *list, int delpos, int ndel ) +{ + if( debug ) + { + // give ourselves a new array containing the deleted elements + int *deleted = malloc( nel * sizeof(int) ); + assert( deleted != NULL ); + int ndeleted = 0; + + for( int i=delpos; i mb ) + { + if( debug ) + { + printf( "debug: element %d explodes\n", b ); + } + rempos = pos+1; + } else + { + if( debug ) + { + printf( "debug: element %d explodes\n", a ); + } + } + + // Remove nrem elements starting at rempos from list[nel] + nel = remove_elements( nel, list, rempos, nrem ); + + if( debug ) + { + printf( "end of round %d: list = ", round ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + } + + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + return 0; +} diff --git a/challenge-210/duncan-c-white/C/parseints.c b/challenge-210/duncan-c-white/C/parseints.c new file mode 100644 index 0000000000..3e820eb334 --- /dev/null +++ b/challenge-210/duncan-c-white/C/parseints.c @@ -0,0 +1,114 @@ +// Simple routine to parse one or more arguments, +// looking for individual ints or comma-separated +// lists of ints. +// + +#include +#include +#include +#include +#include +#include + +#include "args.h" +#include "printarray.h" +#include "parseints.h" + +typedef struct +{ + int nel; // current number of elements + int maxel; // maximum number of elements allocated + int *list; // malloc()d list of integers +} intlist; + + +// +// intlist il.. then initialize il.. then: +// add_one( element, &il ); +// +static void add_one( int x, intlist *p ) +{ + if( p->nel > p->maxel ) + { + p->maxel += 128; + p->list = realloc( p->list, p->maxel ); + assert( p->list != NULL ); + } + #if 0 + if( debug ) + { + printf( "PIA: appending %d to result at " + "pos %d\n", x, p->nel ); + } + #endif + p->list[p->nel++] = x; +} + + +// +// intlist il.. then initialize il.. then: +// add_one_arg( argstr, &il ); +// +static void add_one_arg( char *argstr, intlist *p ) +{ + int x; + if( !check_int(argstr,&x) ) + { + fprintf( stderr, "PIA: arg %s must be int\n", argstr ); + exit(1); + } + add_one( x, p ); +} + + +// +// int nel; +// int *ilist = parse_int_args( argc, argv, argno, &nel ); +// process all arguments argv[argno..argc-1], extracting either +// single ints or comma-separated lists of ints from those arguments, +// accumulate all integers in a dynarray list, storing the total number +// of elements in nel. This list must be freed by the caller. +// Note that the list of elements used to be terminated by a -1 value, +// but I've commented this out from now on. +// +int *parse_int_args( int argc, char **argv, int argno, int *nel ) +{ + int *result = malloc( 128 * sizeof(int) ); + assert( result != NULL ); + intlist il = { 0, 128, result }; + + #if 0 + if( debug ) + { + printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 ); + } + #endif + for( int i=argno; i +#include + + +// print_int_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a separated +// list onto outfile with lines <= maxw chars long. +// produces a whole number of lines of output - without the trailing '\n' +void print_int_array( int maxw, int nel, int *results, char sep, FILE *out ) +{ + int linelen = 0; + for( int i=0; i maxw ) + { + fputc( '\n', out ); + linelen = 0; + } else if( i>0 ) + { + fputc( ' ', out ); + linelen++; + } + + linelen += len; + fprintf( out, "%s", buf ); + if( i0 ) + //{ + // fputc( '\n', out ); + //} +} diff --git a/challenge-210/duncan-c-white/C/printarray.h b/challenge-210/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-210/duncan-c-white/C/printarray.h @@ -0,0 +1 @@ +extern void print_int_array( int maxw, int nel, int * results, char sep, FILE * out ); diff --git a/challenge-210/duncan-c-white/README b/challenge-210/duncan-c-white/README index a7d82c8b31..6013657b55 100644 --- a/challenge-210/duncan-c-white/README +++ b/challenge-210/duncan-c-white/README @@ -1,71 +1,90 @@ -Task 1: Special Bit Characters +Task 1: 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] -> "b" -[1, 1] -> "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. Example 1 - Input: @bits = (1, 0, 0) - Output: 1 + Input: @int = (2, 3, 1) + Output: 6 - The given array bits can be decoded as 2-bits character (10) followed - by 1-bit character (0). + First we delete 2 and that would also delete 1 and 3. So the maximum + points we get is 6. Example 2 - Input: @bits = (1, 1, 1, 0) - Output: 0 - - Possible decode can be 2-bits character (11) followed by 2-bits - character (10) i.e. the last character is not 1-bit character. - -MY NOTES: very easy. decode string then check last letter of decoded version. -I wonder if there's a way of decoding-and-checking together, though. + 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. + +MY NOTES: very easy recursive "all paths" solution with accumulating points +so far, tracking the best (max) complete path found as we go. + +PS: Thinking about this AFTER writing the code, all paths necessarily lead +to the same total sum, as whenever we delete some elements we add them to +the sum, so once we've deleted all elements the total sum is necessarily +the sum of the original elements. So the "find all paths and pick the max +sum" logic is completely unnecessary. Finding any single path and it's score +would do. Hence, I don't understand the emphasis in the question above on the +"write a script to get the maximum points" wording, and why the examples above +show a particular path if any path would do.. It's possible that the question +meant "pick a path that leads to a solution in the shortest number of kaw +rounds" - that's compatible with example 1. But dammit if that's what was +meant, the question should have been clearer. So I'm leaving my code +alone as it answers the question well enough. GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C (look in the C directory for that). -Task 2: Merge Account +Task 2: Number Collision -You are given an array of accounts i.e. name with list of email addresses. +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. -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. +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: @accounts = [ ["A", "a1@a.com", "a2@a.com"], - ["B", "b1@b.com"], - ["A", "a3@a.com", "a1@a.com"] ] - ] + Input: @list = (2, 3, -1) + Output: (2, 3) - Output: [ ["A", "a1@a.com", "a2@a.com", "a3@a.com"], - ["B", "b1@b.com"] ] + The numbers 3 and -1 collide and -1 explodes in the end. So we are + left with (2, 3). Example 2: - Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"], - ["B", "b1@b.com"], - ["A", "a3@a.com"], - ["B", "b2@b.com", "b1@b.com"] ] + 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: () - Output: [ ["A", "a1@a.com", "a2@a.com"], - ["A", "a3@a.com"], - ["B", "b1@b.com", "b2@b.com"] ] + The numbers 1 and -1 both collide and explode. Nothing left in the end. -MY NOTES: fiddly and rather inelegant, especially only being allowed to -merge two entries if the intersection of the email lists is non empty. -Will also need to choose an input format, how about a list of words of the -form A:a1@a.com,a2@a.com, B:b1@b.com, A:a3@a.com and B:b2@b.com,b1@b.com +MY NOTES: very unclear explanation, but stripped of all the "numbers moving +left and right" stuff this seems straightforward (I think): find pairs of +adjacent numbers Pos,Neg where Pos>0 and Neg<0 and remove Pos or Neg depending +on min(abs(Pos),abs(Neg)). Then repeat. I don't know whether one round should +find all such immediate (Pos,Neg) pairs, as all the examples show only one +explosion per round. Or perhaps that doesn't matter, as it's just more rounds. -(TODO) GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C -(TODO) (look in the C directory for that) +GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C +(look in the C directory for that) diff --git a/challenge-210/duncan-c-white/perl/ch-1.pl b/challenge-210/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..8e9a1519d6 --- /dev/null +++ b/challenge-210/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl +# +# Task 1: Kill and Win +# +# 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. +# +# MY NOTES: very easy recursive solution with accumulating points so far. +# Reduce search space by picking each DISTINCT value at each stage, +# not EACH value. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C +# (look in the C directory for that). +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use List::Util qw(max sum); +use Data::Dumper; + +my $debug=0; +die "Usage: kaw [--debug] list-of-ints\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +say "list: ", join(',',@list) if $debug; + +my $max = 0; + + +# +# findall( $sumsofar, @list ); +# Given a sumsofar and a list, find all "kill-and-win" +# paths through the list, updating $max with the maximum +# of all total sums of each complete path. +# +sub findall +{ + my( $sumsofar, @list ) = @_; + if( @list == 0 ) + { + $max = max( $max, $sumsofar ); + return; + } + # find all distinct values in list + my %set = map { $_ => 1 } @list; + foreach my $v (keys %set) + { + # ok, pick $v.. + + # kaw rule: remove all $v-1 and $v+1 values + my @newl = grep { $_ != $v-1 && $_ != $v+1 } @list; + + # and ONE $v value + my $nvseen = 0; + @newl = grep { $_ != $v || ( $_ == $v && $nvseen++ > 0) } @newl; + + my @deleted = grep { $_ == $v-1 || $_ == $v+1 } @list; + push @deleted, $v; + my $news = $sumsofar + sum(@deleted); + say "debug: kaw: list=", join(',',@list), + ", v=$v, deleted=", join(',',@deleted), + ", newl=", join(',',@newl), ", newsum=$news" if $debug; + findall( $news, @newl ); + } +} + + +findall( 0, @list ); +say $max; diff --git a/challenge-210/duncan-c-white/perl/ch-2.pl b/challenge-210/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..57c5c3e7c9 --- /dev/null +++ b/challenge-210/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl +# +# Task 2: Number Collision +# +# 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. +# +# MY NOTES: very unclear explanation, but stripped of all the "numbers moving +# left and right" stuff this seems straightforward (I think): find pairs of +# adjacent numbers Pos,Neg where Pos>0 and Neg<0 and remove Pos or Neg depending +# on min(Pos,abs(Neg)). Then repeat. I don't know whether one round should +# find all such immediate (Pos,Neg) pairs, as all the examples show only one +# explosion per round. Or perhaps that doesn't matter, it's just more rounds. +# +# PLEASE NOTE: Getopt::Long has an unfortunate side-effect - -ve numbers are +# parsed as flags, so I've commented out the -d parsing.. If you want debug +# output, edit the setting of $debug below. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C +# (look in the C directory for that) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Data::Dumper; + +my $debug=1; +#die "Usage: collide [--debug] -- intlist\n" +# unless GetOptions( "debug"=>\$debug ) && @ARGV > 0; + +die "Usage: collide intlist\n" unless @ARGV > 0; + +my @list = split( /,/, join(',',@ARGV) ); + +say "list: ", join(',',@list) if $debug; + +# +# my $pos = find_pos_neg( @list ); +# Search in the given @list for a positive number +# immediately followed by a negative number. +# If you find such a pair return the position of +# the positive number. +# If there is no such pair, return -1. +# +sub find_pos_neg (@) +{ + my( @list ) = @_; + for( my $i=0; $i<@list-1; $i++ ) + { + return $i if $list[$i]>=0 && $list[$i+1]<0; + } + return -1; +} + + +for( my $round = 1; ; $round++ ) +{ + my $pos = find_pos_neg( @list ); + say "round $round: list = ", join(',',@list), " pos = $pos" if $debug; + last if $pos == -1; + + my $a = $list[$pos]; + my $b = $list[$pos+1]; + say "debug: elements $a and $b collide" if $debug; + my $ma = $a; + my $mb = abs($b); + my $rempos = $pos; # remove which starting position? + my $nrem = 1; # how many to remove? + if( $ma == $mb ) # remove both + { + say "debug: both elements $a and $b explode" if $debug; + $rempos = $pos; + $nrem = 2; + } elsif( $ma > $mb ) + { + say "debug: element $b explodes" if $debug; + $rempos = $pos+1; + } else + { + say "debug: element $a explodes" if $debug; + } + my @removed = splice( @list, $rempos, $nrem, () ); + say "debug: removing $nrem elements starting at pos $rempos: ", join(',',@removed) if $debug; + say "end of round $round: list = ", join(',',@list) if $debug; + #die; +} + +say join(',',@list); -- cgit From 8995f1e0c60ae53e54c0e4cd05dfa27cd3f1a841 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 4 Apr 2023 14:14:28 +0100 Subject: - Added solutions by Duncan C. White. - Added solutions by Laurent Rosenfeld. --- challenge-210/laurent-rosenfeld/blog.txt | 1 + challenge-210/laurent-rosenfeld/perl/ch-1.pl | 10 + challenge-210/laurent-rosenfeld/raku/ch-1.raku | 8 + challenge-210/laurent-rosenfeld/raku/ch-2.raku | 39 + stats/pwc-current.json | 278 +- stats/pwc-language-breakdown-summary.json | 64 +- stats/pwc-language-breakdown.json | 8214 ++++++++++++------------ stats/pwc-leaders.json | 764 +-- stats/pwc-summary-1-30.json | 122 +- stats/pwc-summary-121-150.json | 34 +- stats/pwc-summary-151-180.json | 40 +- stats/pwc-summary-181-210.json | 48 +- stats/pwc-summary-211-240.json | 104 +- stats/pwc-summary-241-270.json | 36 +- stats/pwc-summary-271-300.json | 56 +- stats/pwc-summary-31-60.json | 106 +- stats/pwc-summary-61-90.json | 38 +- stats/pwc-summary-91-120.json | 100 +- stats/pwc-summary.json | 1788 +++--- 19 files changed, 5973 insertions(+), 5877 deletions(-) create mode 100644 challenge-210/laurent-rosenfeld/blog.txt create mode 100644 challenge-210/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-210/laurent-rosenfeld/raku/ch-1.raku create mode 100644 challenge-210/laurent-rosenfeld/raku/ch-2.raku diff --git a/challenge-210/laurent-rosenfeld/blog.txt b/challenge-210/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..7021c9a3f8 --- /dev/null +++ b/challenge-210/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/04/perl-weekly-challenge-210-kill-and-win-and-number-collision.html diff --git a/challenge-210/laurent-rosenfeld/perl/ch-1.pl b/challenge-210/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..39a6322bbb --- /dev/null +++ b/challenge-210/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,10 @@ +sub sum_deleted_digits { + # we can always delete all digits + my $sum = 0; + $sum += $_ for @_; + return $sum; +} + +for my $test ([<2 3 1>], [<1 1 2 2 2 3>]) { + printf "%-15s => %d \n", "@$test", sum_deleted_digits @$test; +} diff --git a/challenge-210/laurent-rosenfeld/raku/ch-1.raku b/challenge-210/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..fae3c92f3e --- /dev/null +++ b/challenge-210/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,8 @@ +sub sum-deleted-digits (@in) { + # we can always delete all digits + return [+] @in; +} + +for <2 3 1>, <1 1 2 2 2 3> -> @test { + say "@test[]".fmt("%-15s => "), sum-deleted-digits @test; +} diff --git a/challenge-210/laurent-rosenfeld/raku/ch-2.raku b/challenge-210/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..af43cb332c --- /dev/null +++ b/challenge-210/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,39 @@ +sub number-collision (@in-array) { + my @in = @in-array; + loop { + return () if @in.elems == 0; + my @temp; + for 0..^@in.end -> $i { + if @in[$i] > 0 { + if @in[$i+1] > 0 { + push @temp, @in[$i]; + } else { + next if abs(@in[$i]) == abs(@in[$i+1]); + push @temp, + abs(@in[$i]) > abs(@in[$i+1]) ?? + @in[$i] !! @in[$i+1]; + } + } elsif @in[$i] < 0 { + push @temp, @in[$i] and next + unless @in[$i-1]:exists; + if @in[$i-1] < 0 { + push @temp, @in[$i]; + } else { + shift @temp and next + if abs(@in[$i]) == abs(@in[$i+1]); + @temp[*-1] = + abs(@in[$i]) > abs(@in[$i-1]) ?? + @in[$i] !! @in[$i-1]; + } + } else { # @in[$i] == 0 + push @temp, @in[$i]; + } + } + return @temp if @temp.all > 0 or @temp.all < 0; + @in = @temp; + } +} + +for <2 3 -1>, <3 2 -4>, <1 -1> -> @test { + say "@test[]".fmt("%-10s => "), number-collision @test; +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index de1c99e233..0d8713f994 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,21 +1,16 @@ { + "tooltip" : { + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
", + "followPointer" : 1 + }, "title" : { "text" : "The Weekly Challenge - 210" }, - "chart" : { - "type" : "column" - }, - "legend" : { - "enabled" : 0 - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, "drilldown" : { "series" : [ { + "id" : "Arne Sommer", "name" : "Arne Sommer", "data" : [ [ @@ -26,12 +21,9 @@ "Blog", 1 ] - ], - "id" : "Arne Sommer" + ] }, { - "id" : "Athanasius", - "name" : "Athanasius", "data" : [ [ "Perl", @@ -41,27 +33,29 @@ "Raku", 1 ] - ] + ], + "name" : "Athanasius", + "id" : "Athanasius" }, { - "id" : "Avery Adams", "data" : [ [ "Perl", 2 ] ], + "id" : "Avery Adams", "name" : "Avery Adams" }, { "name" : "Bob Lied", + "id" : "Bob Lied", "data" : [ [ "Perl", 2 ] - ], - "id" : "Bob Lied" + ] }, { "id" : "Carlos Oliveira", @@ -75,17 +69,17 @@ }, { "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung", "data" : [ [ "Perl", 1 ] - ], - "id" : "Cheok-Yin Fung" + ] }, { - "id" : "David Ferrone", "name" : "David Ferrone", + "id" : "David Ferrone", "data" : [ [ "Perl", @@ -94,16 +88,27 @@ ] }, { - "id" : "E. Choroba", - "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Duncan C. White", + "id" : "Duncan C. White" }, { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "id" : "Flavio Poletti", "name" : "Flavio Poletti", "data" : [ [ @@ -118,8 +123,7 @@ "Blog", 2 ] - ], - "id" : "Flavio Poletti" + ] }, { "data" : [ @@ -136,8 +140,8 @@ "id" : "James Smith" }, { - "id" : "Jan Krnavek", "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", @@ -146,17 +150,36 @@ ] }, { - "id" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] ], + "id" : "Jorg Sommrey", "name" : "Jorg Sommrey" }, + { + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, { "name" : "Luca Ferrari", + "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -166,38 +189,37 @@ "Blog", 6 ] - ], - "id" : "Luca Ferrari" + ] }, { + "id" : "Mariano Spadaccini", + "name" : "Mariano Spadaccini", "data" : [ [ "Perl", 1 ] - ], - "name" : "Mariano Spadaccini", - "id" : "Mariano Spadaccini" + ] }, { "id" : "Mark Anderson", + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "name" : "Mark Anderson" + ] }, { - "id" : "Matthew Neleigh", - "name" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh" }, { "data" : [ @@ -206,20 +228,22 @@ 2 ] ], - "name" : "Matthias Muth", - "id" : "Matthias Muth" + "id" : "Matthias Muth", + "name" : "Matthias Muth" }, { - "name" : "Paulo Custodio", "data" : [ [ "Perl", 2 ] ], - "id" : "Paulo Custodio" + "id" : "Paulo Custodio", + "name" : "Paulo Custodio" }, { + "name" : "Peter Campbell Smith", + "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -229,18 +253,16 @@ "Blog", 1 ] - ], - "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith" + ] }, { - "id" : "Peter Meszaros", "data" : [ [ "Perl", 2 ] ], + "id" : "Peter Meszaros", "name" : "Peter Meszaros" }, { @@ -272,8 +294,8 @@ ] }, { - "id" : "Robert DiCicco", "name" : "Robert DiCicco", + "id" : "Robert DiCicco", "data" : [ [ "Perl", @@ -286,17 +308,18 @@ ] }, { - "id" : "Robert Ransbottom", - "name" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Robert Ransbottom", + "id" : "Robert Ransbottom" }, { "id" : "Roger Bell_West", + "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -306,21 +329,19 @@ "Raku", 2 ] - ], - "name" : "Roger Bell_West" + ] }, { + "name" : "Shimon Bollinger", "id" : "Shimon Bollinger", "data" : [ [ "Raku", 1 ] - ], - "name" : "Shimon Bollinger" + ] }, { - "name" : "Simon Green", "data" : [ [ "Perl", @@ -331,21 +352,20 @@ 1 ] ], + "name" : "Simon Green", "id" : "Simon Green" }, { - "id" : "Solathian", "data" : [ [ "Perl", 2 ] ], + "id" : "Solathian", "name" : "Solathian" }, { - "id" : "Thomas Kohler", - "name" : "Thomas Kohler", "data" : [ [ "Perl", @@ -355,7 +375,9 @@ "Blog", 2 ] - ] + ], + "name" : "Thomas Kohler", + "id" : "Thomas Kohler" }, { "id" : "Ulrich Rieke", @@ -372,6 +394,7 @@ ] }, { + "id" : "W. Luis Mochan", "name" : "W. Luis Mochan", "data" : [ [ @@ -382,23 +405,38 @@ "Blog", 1 ] - ], - "id" : "W. Luis Mochan" + ] } ] }, + "chart" : { + "type" : "column" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "subtitle" : { + "text" : "[Champions: 33] Last updated at 2023-04-04 13:09:56 GMT" + }, + "legend" : { + "enabled" : 0 + }, "plotOptions" : { "series" : { "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 + "enabled" : 1, + "format" : "{point.y}" }, "borderWidth" : 0 } }, + "xAxis" : { + "type" : "category" + }, "series" : [ { - "colorByPoint" : 1, "data" : [ { "y" : 3, @@ -412,13 +450,13 @@ }, { "y" : 2, - "drilldown" : "Avery Adams", - "name" : "Avery Adams" + "name" : "Avery Adams", + "drilldown" : "Avery Adams" }, { "y" : 2, - "name" : "Bob Lied", - "drilldown" : "Bob Lied" + "drilldown" : "Bob Lied", + "name" : "Bob Lied" }, { "y" : 2, @@ -431,8 +469,13 @@ "y" : 1 }, { + "y" : 2, "drilldown" : "David Ferrone", - "name" : "David Ferrone", + "name" : "David Ferrone" + }, + { + "name" : "Duncan C. White", + "drilldown" : "Duncan C. White", "y" : 2 }, { @@ -441,24 +484,29 @@ "name" : "E. Choroba" }, { - "drilldown" : "Flavio Poletti", + "y" : 6, "name" : "Flavio Poletti", - "y" : 6 + "drilldown" : "Flavio Poletti" }, { + "y" : 3, "name" : "James Smith", - "drilldown" : "James Smith", - "y" : 3 + "drilldown" : "James Smith" }, { "y" : 1, - "name" : "Jan Krnavek", - "drilldown" : "Jan Krnavek" + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek" }, { - "y" : 2, + "name" : "Jorg Sommrey", "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey" + "y" : 2 + }, + { + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld", + "y" : 4 }, { "name" : "Luca Ferrari", @@ -466,19 +514,19 @@ "y" : 8 }, { - "y" : 1, "name" : "Mariano Spadaccini", - "drilldown" : "Mariano Spadaccini" + "drilldown" : "Mariano Spadaccini", + "y" : 1 }, { + "y" : 2, "drilldown" : "Mark Anderson", - "name" : "Mark Anderson", - "y" : 2 + "name" : "Mark Anderson" }, { - "drilldown" : "Matthew Neleigh", + "y" : 2, "name" : "Matthew Neleigh", - "y" : 2 + "drilldown" : "Matthew Neleigh" }, { "y" : 2, @@ -486,88 +534,78 @@ "drilldown" : "Matthias Muth" }, { + "y" : 2, "name" : "Paulo Custodio", - "drilldown" : "Paulo Custodio", - "y" : 2 + "drilldown" : "Paulo Custodio" }, { - "drilldown" : "Peter Campbell Smith", "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith", "y" : 3 }, { - "drilldown" : "Peter Meszaros", + "y" : 2, "name" : "Peter Meszaros", - "y" : 2 + "drilldown" : "Peter Meszaros" }, { - "drilldown" : "Pip Stuart", + "y" : 4, "name" : "Pip Stuart", - "y" : 4 + "drilldown" : "Pip Stuart" }, { - "y" : 3, "name" : "Robbie Hatley", - "drilldown" : "Robbie Hatley" + "drilldown" : "Robbie Hatley", + "y" : 3 }, { - "y" : 2, + "name" : "Robert DiCicco", "drilldown" : "Robert DiCicco", - "name" : "Robert DiCicco" + "y" : 2 }, { - "y" : 2, + "name" : "Robert Ransbottom", "drilldown" : "Robert Ransbottom", - "name" : "Robert Ransbottom" + "y" : 2 }, { - "y" : 4, "name" : "Roger Bell_West", - "drilldown" : "Roger Bell_West" + "drilldown" : "Roger Bell_West", + "y" : 4 }, { - "y" : 1, + "name" : "Shimon Bollinger", "drilldown" : "Shimon Bollinger", - "name" : "Shimon Bollinger" + "y" : 1 }, { - "drilldown" : "Simon Green", "name" : "Simon Green", + "drilldown" : "Simon Green", "y" : 3 }, { - "name" : "Solathian", "drilldown" : "Solathian", + "name" : "Solathian", "y" : 2 }, { - "y" : 4, + "name" : "Thomas Kohler", "drilldown" : "Thomas Kohler", - "name" : "Thomas Kohler" + "y" : 4 }, { - "y" : 4, + "drilldown" : "Ulrich Rieke", "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke" + "y" : 4 }, { - "name" : "W. Luis Mochan", "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", "y" : 3 } ], + "colorByPoint" : 1, "name" : "The Weekly Challenge - 210" } - ], - "xAxis" : { - "type" : "category" - }, - "subtitle" : { - "text" : "[Champions: 31] Last updated at 2023-04-03 00:55:18 GMT" - }, - "tooltip" : { - "pointFormat" : "{point.name}: {point.y:f}
", - "headerFormat" : "{series.name}
", - "followPointer" : 1 - } + ] } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 33a520d813..f2038ab4a2 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,43 +1,46 @@ { - "chart" : { - "type" : "column" - }, - "title" : { - "text" : "The Weekly Challenge Contributions [2019 - 2023]" - }, "series" : [ { - "dataLabels" : { - "align" : "right", - "y" : 10, - "enabled" : "true", - "color" : "#FFFFFF", - "rotation" : -90, - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - }, - "format" : "{point.y:.0f}" - }, "data" : [ [ "Blog", - 3444 + 3445 ], [ "Perl", - 10628 + 10631 ], [ "Raku", - 6221 + 6223 ] ], + "dataLabels" : { + "format" : "{point.y:.0f}", + "rotation" : -90, + "align" : "right", + "color" : "#FFFFFF", + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + }, + "y" : 10, + "enabled" : "true" + }, "name" : "Contributions" } ], - "legend" : { - "enabled" : "false" + "xAxis" : { + "labels" : { + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + } + }, + "type" : "category" + }, + "chart" : { + "type" : "column" }, "yAxis" : { "title" : { @@ -45,19 +48,16 @@ }, "min" : 0 }, + "legend" : { + "enabled" : "false" + }, "subtitle" : { - "text" : "Last updated at 2023-04-03 00:55:17 GMT" + "text" : "Last updated at 2023-04-04 13:09:56 GMT" }, "tooltip" : { "pointFormat" : "{point.y:.0f}" }, - "xAxis" : { - "labels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - } - }, - "type" : "category" + "title" : { + "text" : "The Weekly Challenge Contributions [2019 - 2023]" } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index f265d8fab1..a41e7b8421 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,3825 +1,7 @@ { - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } - }, - "drilldown" : { - "series" : [ - { - "data" : [ - [ - "Perl", - 105 - ], - [ - "Raku", - 47 - ], - [ - "Blog", - 11 - ] - ], - "name" : "001", - "id" : "001" - }, - { - "name" : "002", - "data" : [ - [ - "Perl", - 83 - ], - [ - "Raku", - 36 - ], - [ - "Blog", - 10 - ] - ], - "id" : "002" - }, - { - "name" : "003", - "data" : [ - [ - "Perl", - 48 - ], - [ - "Raku", - 30 - ], - [ - "Blog", - 9 - ] - ], - "id" : "003" - }, - { - "id" : "004", - "data" : [ - [ - "Perl", - 60 - ], - [ - "Raku", - 33 - ], - [ - "Blog", - 10 - ] - ], - "name" : "004" - }, - { - "name" : "005", - "data" : [ - [ - "Perl", - 42 - ], - [ - "Raku", - 26 - ], - [ - "Blog", - 12 - ] - ], - "id" : "005" - }, - { - "id" : "006", - "data" : [ - [ - "Perl", - 36 - ], - [ - "Raku", - 18 - ], - [ - "Blog", - 7 - ] - ], - "name" : "006" - }, - { - "name" : "007", - "data" : [ - [ - "Perl", - 36 - ], - [ - "Raku", - 23 - ], - [ - "Blog", - 10 - ] - ], - "id" : "007" - }, - { - "id" : "008", - "data" : [ - [ - "Perl", - 48 - ], - [ - "Raku", - 22 - ], - [ - "Blog", - 12 - ] - ], - "name" : "008" - }, - { - "name" : "009", - "data" : [ - [ - "Perl", - 46 - ], - [ - "Raku", - 21 - ], - [ - "Blog", - 13 - ] - ], - "id" : "009" - }, - { - "name" : "010", - "data" : [ - [ - "Perl", - 39 - ], - [ - "Raku", - 19 - ], - [ - "Blog", - 11 - ] - ], - "id" : "010" - }, - { - "id" : "011", - "name" : "011", - "data" : [ - [ - "Perl", - 51 - ], - [ - "Raku", - 28 - ], - [ - "Blog", - 10 - ] - ] - }, - { - "id" : "012", - "data" : [ - [ - "Perl", - 51 - ], - [ - "Raku", - 30 - ], - [ - "Blog", - 11 - ] - ], - "name" : "012" - }, - { - "data" : [ - [ - "Perl", - 49 - ], - [ - "Raku", - 25 - ], - [ - "Blog", - 13 - ] - ], - "name" : "013", - "id" : "013" - }, - { - "id" : "014", - "name" : "014", - "data" : [ - [ - "Perl", - 56 - ], - [ - "Raku", - 31 - ], - [ - "Blog", - 15 - ] - ] - }, - { - "id" : "015", - "name" : "015", - "data" : [ - [ - "Perl", - 58 - ], - [ - "Raku", - 28 - ], - [ - "Blog", - 15 - ] - ] - }, - { - "id" : "016", - "data" : [ - [ - "Perl", - 37 - ], - [ - "Raku", - 23 - ], - [ - "Blog", - 12 - ] - ], - "name" : "016" - }, - { - "id" : "017", - "name" : "017", - "data" : [ - [ - "Perl", - 47 - ], - [ - "Raku", - 27 - ], - [ - "Blog", - 12 - ] - ] - }, - { - "name" : "018", - "data" : [ - [ - "Perl", - 38 - ], - [ - "Raku", - 31 - ], - [ - "Blog", - 14 - ] - ], - "id" : "018" - }, - { - "data" : [ - [ - "Perl", - 58 - ], - [ - "Raku", - 34 - ], - [ - "Blog", - 13 - ] - ], - "name" : "019", - "id" : "019" - }, - { - "id" : "020", - "name" : "020", - "data" : [ - [ - "Perl", - 53 - ], - [ -