aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2025-10-11 20:17:23 -0600
committerRyan Thompson <i@ry.ca>2025-10-11 20:17:23 -0600
commit4a19245c74df65cb7f717c5d4737d385349faa4b (patch)
treea00cb074d4a5fb86b47c5e3baf170db9287e7a64
parent583c623790665395169637e0bde6a900297c318c (diff)
downloadperlweeklychallenge-club-4a19245c74df65cb7f717c5d4737d385349faa4b.tar.gz
perlweeklychallenge-club-4a19245c74df65cb7f717c5d4737d385349faa4b.tar.bz2
perlweeklychallenge-club-4a19245c74df65cb7f717c5d4737d385349faa4b.zip
rjt's week 342 solutions, blog, and tests
-rw-r--r--challenge-342/ryan-thompson/README.md8
-rw-r--r--challenge-342/ryan-thompson/blog.txt1
-rw-r--r--challenge-342/ryan-thompson/perl/ch-1.pl15
-rw-r--r--challenge-342/ryan-thompson/perl/ch-2.pl18
-rw-r--r--challenge-342/ryan-thompson/perl/t/ch-1.t18
-rw-r--r--challenge-342/ryan-thompson/perl/t/ch-2.t13
6 files changed, 69 insertions, 4 deletions
diff --git a/challenge-342/ryan-thompson/README.md b/challenge-342/ryan-thompson/README.md
index 770aebaa99..ec7aeb0f5f 100644
--- a/challenge-342/ryan-thompson/README.md
+++ b/challenge-342/ryan-thompson/README.md
@@ -1,18 +1,18 @@
# Ryan Thompson
-## Week 341 Solutions
+## Week 342 Solutions
-### Task 1 › Broken Keyboard
+### Task 1 › Balanced Strings
* [Perl](perl/ch-1.pl)
-### Task 2 › Reverse Prefix
+### Task 2 › Max Score
* [Perl](perl/ch-2.pl)
## Blog
- * [Brken Keybard and Reverse Prefix](https://ry.ca/2025/10/brken-keybards-reverse-prefixes/)
+ * [Perfectly Balanced and Pointlessly Optimized](https://ry.ca/2025/10/pwc-342-balanced-optimized/)
## Tests
diff --git a/challenge-342/ryan-thompson/blog.txt b/challenge-342/ryan-thompson/blog.txt
new file mode 100644
index 0000000000..d125998ee0
--- /dev/null
+++ b/challenge-342/ryan-thompson/blog.txt
@@ -0,0 +1 @@
+https://ry.ca/2025/10/pwc-342-balanced-optimized/
diff --git a/challenge-342/ryan-thompson/perl/ch-1.pl b/challenge-342/ryan-thompson/perl/ch-1.pl
new file mode 100644
index 0000000000..535bada037
--- /dev/null
+++ b/challenge-342/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+
+use v5.38;
+use List::Util qw< mesh >;
+no warnings 'uninitialized';
+
+sub balanced {
+ die "`$_[0]' must contain [a-z0-9]" if $_[0] !~ /^[a-z0-9]*$/;
+ my ($one, $two) = ([], []);
+
+ push @{ /\d/ ? $one : $two }, $_ for sort split '', $_[0];
+ return "" if abs(@$one - @$two) > 1; # C'est impossible
+
+ join '', mesh sort { @$b <=> @$a } $one, $two;
+}
diff --git a/challenge-342/ryan-thompson/perl/ch-2.pl b/challenge-342/ryan-thompson/perl/ch-2.pl
new file mode 100644
index 0000000000..e6a8fb3dc4
--- /dev/null
+++ b/challenge-342/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+
+use v5.38;
+use List::Util qw< max >;
+
+sub max_score {
+ return 0 if $_[0] eq '';
+ my ($l, $r) = split //, $_[0], 2;
+ my $s = !$l + $r =~ tr/1//;
+ max 0+$s, map { $s += $_ ? -1 : 1 } split //,substr $r,0,-1;
+}
+
+# Only a few % faster on larger strings, and a lot less readable.
+sub max_score_ugly_fast {
+ return 0 if $_[0] eq '';
+ my $s = !substr($_[0],0,1) + substr($_[0],1) =~ tr/1//;
+ max 0+$s, map { $s += substr($_[0],$_,1) ? -1 : 1 } 1..length($_[0])-2;
+}
diff --git a/challenge-342/ryan-thompson/perl/t/ch-1.t b/challenge-342/ryan-thompson/perl/t/ch-1.t
new file mode 100644
index 0000000000..8b4acd53d2
--- /dev/null
+++ b/challenge-342/ryan-thompson/perl/t/ch-1.t
@@ -0,0 +1,18 @@
+#!perl
+
+use v5.38;
+
+require './ch-1.pl';
+
+use Test2::V0;
+
+is balanced('a0b1c2'), '0a1b2c';
+is balanced('abc12'), 'a1b2c';
+is balanced('0a2b1c3'),'0a1b2c3';
+is balanced('1a23'), '';
+is balanced('ab123'), '1a2b3';
+is balanced($_), $_ for '', '1', 'a';
+is balanced('unbalanced123'), '';
+ok dies { balanced('ab!12') };
+
+done_testing;
diff --git a/challenge-342/ryan-thompson/perl/t/ch-2.t b/challenge-342/ryan-thompson/perl/t/ch-2.t
new file mode 100644
index 0000000000..528856b7a2
--- /dev/null
+++ b/challenge-342/ryan-thompson/perl/t/ch-2.t
@@ -0,0 +1,13 @@
+#!perl
+
+require './ch-2.pl';
+use Test2::V0;
+
+is max_score('0011'), 4;
+is max_score('0000'), 3;
+is max_score('1111'), 3;
+is max_score('0101'), 3;
+is max_score('011101'), 5;
+is max_score(''), 0;
+
+done_testing;