From dd4a57665bdebce636af0c254d624d8f36805802 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 20 Mar 2023 14:08:40 -0600 Subject: Solve PWC209 --- challenge-209/wlmb/blog.txt | 2 ++ challenge-209/wlmb/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-209/wlmb/perl/ch-2.pl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 challenge-209/wlmb/blog.txt create mode 100755 challenge-209/wlmb/perl/ch-1.pl create mode 100755 challenge-209/wlmb/perl/ch-2.pl diff --git a/challenge-209/wlmb/blog.txt b/challenge-209/wlmb/blog.txt new file mode 100644 index 0000000000..a1bfa8bd88 --- /dev/null +++ b/challenge-209/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/03/20/PWC209/ + diff --git a/challenge-209/wlmb/perl/ch-1.pl b/challenge-209/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..a2ce3b90a4 --- /dev/null +++ b/challenge-209/wlmb/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# Perl weekly challenge 209 +# Task 1: Special Bit Characters +# +# See https://wlmb.github.io/2023/03/20/PWC209/#task-1-special-bit-characters +use v5.36; +my %patterns; +die <<~"FIN" unless @ARGV; + Usage: $0 S1 [S2...] + to decode the binary sequences S1 S2 + using the code 0->a, 10->b 11->c + FIN + +@patterns{qw(0 10 11)}=qw(a b c); +for(@ARGV){ + my $last; + die "Not a binary pattern: $_\n" unless /^(0|1)+/; #Check input + say " $_ -> ", + ( + map {$last=$patterns{$_}} + grep {length $_} + split /(1.|0)/ + ), + " -> ", + $last eq "a"?1:0; +} diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..50a270aecd --- /dev/null +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +# Perl weekly challenge 209 +# Task 2: Merge Account +# +# See https://wlmb.github.io/2023/03/20/PWC209/#task-2-merge-account +use v5.36; +use English; +my %merge_to; +my %line_of; +my @names; +while(<>){ + chomp; + my ($name, @addresses)=split / /; + $names[$INPUT_LINE_NUMBER] = $name; + for(@addresses){ + $merge_to{$INPUT_LINE_NUMBER} = + defined $merge_to{$line_of{$_}} + ?$merge_to{$line_of{$_}} # previously merged + :$line_of{$_} # or first time merge + if defined $line_of{$_}; # address has been seen before + $line_of{$_}//=$INPUT_LINE_NUMBER; # map to current line if not seen before + } + $merge_to{$.}//=$INPUT_LINE_NUMBER; # merge to itself if not already merged +} +my @addresses = keys %line_of; # distinct addresses +my %merged; # merged accounts +push @{$merged{$merge_to{$line_of{$_}}}}, $_ # add addresses to merged accounts + for @addresses; +# output account name, (one of its) line number(s), addresses +say join " ", $names[$_], "($_):", sort {$a cmp $b} @{$merged{$_}} + for sort {$names[$a] cmp $names[$b] || $a <=> $b} keys %merged; -- cgit From cff5708de2aaa2ac6b2ba4d4fcb53d1f90c1d538 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 20 Mar 2023 17:51:17 -0600 Subject: Simplified solution --- challenge-209/wlmb/perl/ch-2.pl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl index 50a270aecd..1210214470 100755 --- a/challenge-209/wlmb/perl/ch-2.pl +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -14,9 +14,7 @@ while(<>){ $names[$INPUT_LINE_NUMBER] = $name; for(@addresses){ $merge_to{$INPUT_LINE_NUMBER} = - defined $merge_to{$line_of{$_}} - ?$merge_to{$line_of{$_}} # previously merged - :$line_of{$_} # or first time merge + $merge_to{$line_of{$_}} # previously merged or undef if defined $line_of{$_}; # address has been seen before $line_of{$_}//=$INPUT_LINE_NUMBER; # map to current line if not seen before } -- cgit From ecdc3f8651ad777dfac7a10d4af0aee18ef4f772 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Tue, 21 Mar 2023 12:58:18 -0600 Subject: Correct mistake --- challenge-209/wlmb/perl/ch-2.pl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl index 1210214470..e11b229f2c 100755 --- a/challenge-209/wlmb/perl/ch-2.pl +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -12,13 +12,13 @@ while(<>){ chomp; my ($name, @addresses)=split / /; $names[$INPUT_LINE_NUMBER] = $name; + $merge_to{$INPUT_LINE_NUMBER}=$INPUT_LINE_NUMBER; # merge to itself for(@addresses){ - $merge_to{$INPUT_LINE_NUMBER} = - $merge_to{$line_of{$_}} # previously merged or undef - if defined $line_of{$_}; # address has been seen before - $line_of{$_}//=$INPUT_LINE_NUMBER; # map to current line if not seen before + my $l=$line_of{$_}; + $line_of{$_}=$INPUT_LINE_NUMBER; + # merge overlapping accounts with current one + $merge_to{$l}=$merge_to{$merge_to{$l}}=$INPUT_LINE_NUMBER if defined $l; } - $merge_to{$.}//=$INPUT_LINE_NUMBER; # merge to itself if not already merged } my @addresses = keys %line_of; # distinct addresses my %merged; # merged accounts -- cgit From 0f80fec7fbf53a1da397910e6c95da28ba79ef7b Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Tue, 21 Mar 2023 19:29:06 -0600 Subject: Changed strategy for task 2 --- challenge-209/wlmb/perl/ch-2.pl | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl index e11b229f2c..46ad3198c8 100755 --- a/challenge-209/wlmb/perl/ch-2.pl +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -5,25 +5,20 @@ # See https://wlmb.github.io/2023/03/20/PWC209/#task-2-merge-account use v5.36; use English; -my %merge_to; my %line_of; my @names; while(<>){ chomp; my ($name, @addresses)=split / /; - $names[$INPUT_LINE_NUMBER] = $name; - $merge_to{$INPUT_LINE_NUMBER}=$INPUT_LINE_NUMBER; # merge to itself - for(@addresses){ - my $l=$line_of{$_}; - $line_of{$_}=$INPUT_LINE_NUMBER; - # merge overlapping accounts with current one - $merge_to{$l}=$merge_to{$merge_to{$l}}=$INPUT_LINE_NUMBER if defined $l; - } + next unless $name; # skip empty lines + $names[$INPUT_LINE_NUMBER]=$name; + my ($merged)=grep {defined $_} map {$line_of{$_}} @addresses; + $merged//=$INPUT_LINE_NUMBER; # current line by default + @line_of{@addresses}=($merged) x @addresses; } -my @addresses = keys %line_of; # distinct addresses -my %merged; # merged accounts -push @{$merged{$merge_to{$line_of{$_}}}}, $_ # add addresses to merged accounts - for @addresses; -# output account name, (one of its) line number(s), addresses -say join " ", $names[$_], "($_):", sort {$a cmp $b} @{$merged{$_}} - for sort {$names[$a] cmp $names[$b] || $a <=> $b} keys %merged; +my %accounts; +push @{$accounts{$line_of{$_}}}, $_ for(keys %line_of); +# Sort output by account name and line number, and sort addresses +say "$names[$_] ($_): ", join " ", + sort {$a cmp $b} @{$accounts{$_}} + for sort {$names[$a] cmp $names[$b] || $a <=>$b} keys %accounts; -- cgit From 83c45a8adb310338def62750bf66cb461bfbb52a Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Wed, 22 Mar 2023 08:10:28 -0600 Subject: Fix yet another error --- challenge-209/wlmb/perl/ch-2.pl | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl index 46ad3198c8..ff3285d04a 100755 --- a/challenge-209/wlmb/perl/ch-2.pl +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -5,20 +5,24 @@ # See https://wlmb.github.io/2023/03/20/PWC209/#task-2-merge-account use v5.36; use English; +use List::Util qw(uniq); my %line_of; +my %addresses_of; my @names; while(<>){ chomp; - my ($name, @addresses)=split / /; + # Assume input is of the form: name address1 address2... + my ($name, @addresses)=split ' '; next unless $name; # skip empty lines $names[$INPUT_LINE_NUMBER]=$name; - my ($merged)=grep {defined $_} map {$line_of{$_}} @addresses; - $merged//=$INPUT_LINE_NUMBER; # current line by default - @line_of{@addresses}=($merged) x @addresses; + my @merged=grep {defined $_} map {$line_of{$_}} @addresses; # lines to merge with current + push @addresses, map {@{$addresses_of{$_}}} @merged; # add their addresses + @addresses=uniq @addresses; # avoid repetitions + delete $addresses_of{$_} for @merged; # delete merged lines + @line_of{@addresses}=($INPUT_LINE_NUMBER) x @addresses; # map addresses to line + $addresses_of{$INPUT_LINE_NUMBER}=[@addresses]; # map line to addresses } -my %accounts; -push @{$accounts{$line_of{$_}}}, $_ for(keys %line_of); -# Sort output by account name and line number, and sort addresses +# Output. Sort by account name and line number, and sort addresses say "$names[$_] ($_): ", join " ", - sort {$a cmp $b} @{$accounts{$_}} - for sort {$names[$a] cmp $names[$b] || $a <=>$b} keys %accounts; + sort {$a cmp $b} @{$addresses_of{$_}} + for sort {$names[$a] cmp $names[$b] || $a <=>$b} keys %addresses_of; -- cgit