aboutsummaryrefslogtreecommitdiff
path: root/challenge-331/ysth/perl
diff options
context:
space:
mode:
authorYitzchak Scott-Thoennes <sthoenna@gmail.com>2025-07-27 21:03:54 -0400
committerYitzchak Scott-Thoennes <sthoenna@gmail.com>2025-07-27 21:03:54 -0400
commit00eacff816e458cadb4442e7b2ec596e6bce1a88 (patch)
tree2d87cfdd95bd1878243dd552a7b71c81ed256d03 /challenge-331/ysth/perl
parent99c4e905c974e2e92e238c10cbfc771e89b6cc90 (diff)
downloadperlweeklychallenge-club-00eacff816e458cadb4442e7b2ec596e6bce1a88.tar.gz
perlweeklychallenge-club-00eacff816e458cadb4442e7b2ec596e6bce1a88.tar.bz2
perlweeklychallenge-club-00eacff816e458cadb4442e7b2ec596e6bce1a88.zip
challenge 331: fix task2 solutions
Diffstat (limited to 'challenge-331/ysth/perl')
-rw-r--r--challenge-331/ysth/perl/ch-2.pl35
1 files changed, 32 insertions, 3 deletions
diff --git a/challenge-331/ysth/perl/ch-2.pl b/challenge-331/ysth/perl/ch-2.pl
index 2aebf3e46f..f183cd768c 100644
--- a/challenge-331/ysth/perl/ch-2.pl
+++ b/challenge-331/ysth/perl/ch-2.pl
@@ -1,11 +1,40 @@
use 5.040;
use utf8::all; # utf8 @ARGV
-use Text::Fuzzy ();
+
+# replacement for the pre-perl5.28 stringwise xor that worked on any codepoints (up to uvsize)
+sub bitwise_xor {
+ # copy/stringize arguments so overload/magic only happens once per arg
+ my $a = "$_[0]";
+ my $b = "$_[1]";
+ if (!utf8::is_utf8($a) && !utf8::is_utf8($b)) {
+ no if $] > 5.022, 'feature' => 'bitwise';
+ return $a ^ $b;
+ }
+ if (length $a < length $b) {
+ $a =~ s/./chr(ord $& ^ ord($b =~ m!.!gs && $&))/gse;
+ return $a . substr $b, length $a;
+ }
+ else {
+ $b =~ s/./chr(ord $& ^ ord($a =~ m!.!gs && $&))/gse;
+ return $b . substr $a, length $b;
+ }
+}
sub buddy_strings($string1, $string2) {
- length $string1 == length $string2
- and Text::Fuzzy->new($string1, max => 1, no_exact => 1) == 1
+ if ($string1 eq $string2) {
+ # a doubled character to "swap"
+ return $string1 =~ /(.)\1/s;
+ }
+ else {
+ return
+ # lengths must match
+ length $string1 == length $string2
+ # exactly 2 differing characters, adjacent
+ && bitwise_xor($string1, $string2) =~ /^\0*([^\0])\1\0*\z/
+ # in opposite order in the two strings
+ && substr($string1, $-[1], 2) eq reverse substr($string2, $-[1], 2)
+ }
}
sub main() {