From 3b50c08c0360f94c25df0e78581b0333d07edf4d Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Fri, 16 Feb 2024 08:36:44 -0600 Subject: Add alternative solution --- challenge-256/wlmb/perl/ch-1.pl | 2 +- challenge-256/wlmb/perl/ch-1a.pl | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100755 challenge-256/wlmb/perl/ch-1a.pl diff --git a/challenge-256/wlmb/perl/ch-1.pl b/challenge-256/wlmb/perl/ch-1.pl index 4163c91776..f5da52e2e6 100755 --- a/challenge-256/wlmb/perl/ch-1.pl +++ b/challenge-256/wlmb/perl/ch-1.pl @@ -7,7 +7,7 @@ use v5.36; use List::Util qw(sum0); die <<~"FIN" unless @ARGV; Usage: $0 S1 [S2...] - to pair Si with the reverse SJ and count the resulting pairs + to pair Si with the reverse Sj and count the resulting pairs assuming at most one pair per string. FIN my %count; diff --git a/challenge-256/wlmb/perl/ch-1a.pl b/challenge-256/wlmb/perl/ch-1a.pl new file mode 100755 index 0000000000..8da7ff0e39 --- /dev/null +++ b/challenge-256/wlmb/perl/ch-1a.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 256 +# Task 1: Maximum Pairs +# +# See https://wlmb.github.io/2024/02/12/PWC256/#task-1-maximum-pairs +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + Usage: $0 S1 [S2...] + to pair Si with the reverse Sj and count the resulting pairs + FIN +my %count; +++$count{$_} for (@ARGV); +my $result = (sum0 map { + my $reverse=reverse $_; + my $selfreverse=$_ eq $reverse; + $count{$_}*(($count{$reverse}//0)-$selfreverse) +} keys %count)/2; +say "@ARGV -> $result"; -- cgit