From 8d278f426f8f7358942b1a07cbd15cdf9afd7501 Mon Sep 17 00:00:00 2001 From: Torgny Lyon Date: Fri, 10 Oct 2025 16:30:32 +0200 Subject: Add solutions for week 342 --- challenge-342/torgny-lyon/blog.txt | 1 + challenge-342/torgny-lyon/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-342/torgny-lyon/perl/ch-2.pl | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 challenge-342/torgny-lyon/blog.txt create mode 100755 challenge-342/torgny-lyon/perl/ch-1.pl create mode 100755 challenge-342/torgny-lyon/perl/ch-2.pl diff --git a/challenge-342/torgny-lyon/blog.txt b/challenge-342/torgny-lyon/blog.txt new file mode 100644 index 0000000000..2c287d1630 --- /dev/null +++ b/challenge-342/torgny-lyon/blog.txt @@ -0,0 +1 @@ +https://www.abc.se/~torgny/pwc.html#342 diff --git a/challenge-342/torgny-lyon/perl/ch-1.pl b/challenge-342/torgny-lyon/perl/ch-1.pl new file mode 100755 index 0000000000..563bb3e3f3 --- /dev/null +++ b/challenge-342/torgny-lyon/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +use v5.36; + +use Test::More tests => 5; + +use List::Util qw(mesh); + +sub balance_string { + my @letters = sort $_[0] =~ /[a-z]/g; + my @digits = sort $_[0] =~ /\d/g; + if (@digits == @letters) { + return join q{}, mesh \@digits, \@letters; + } elsif (@digits == @letters + 1) { + return join q{}, grep { defined } mesh \@digits, \@letters; + } elsif (@letters == @digits + 1) { + return join q{}, grep { defined } mesh \@letters, \@digits; + } + q{}; +} + +is(balance_string("a0b1c2"), "0a1b2c"); +is(balance_string("abc12"), "a1b2c"); +is(balance_string("0a2b1c3"), "0a1b2c3"); +is(balance_string("1a23"), q{}); +is(balance_string("ab123"), "1a2b3"); diff --git a/challenge-342/torgny-lyon/perl/ch-2.pl b/challenge-342/torgny-lyon/perl/ch-2.pl new file mode 100755 index 0000000000..25d666b0ef --- /dev/null +++ b/challenge-342/torgny-lyon/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use v5.36; + +use Test::More tests => 5; + +use List::Util qw(max); + +sub get_max_score { + max map { + (() = substr($_[0], 0, $_) =~ /0/g) + (() = substr($_[0], $_) =~ /1/g) + } 1..(length($_[0]) - 1); +} + +is(get_max_score("0011"), 4); +is(get_max_score("0000"), 3); +is(get_max_score("1111"), 3); +is(get_max_score("0101"), 3); +is(get_max_score("011101"), 5); -- cgit