]> www.fi.muni.cz Git - aoc.git/blobdiff - 2022/05.pl
Day 3: regexp-based solution
[aoc.git] / 2022 / 05.pl
index 1a787996af13a87a422ca2f928406a7a406dd300..a7e1f9db71bcd52fbef00ca118a04c726d97e9a8 100755 (executable)
@@ -7,14 +7,10 @@ use experimental 'multidimensional';
 my $sum;
 while (<>) {
        chomp;
-       my $len = length;
-       my ($l, $r) = (substr($_, 0, $len/2), substr($_, $len/2));
-       for my $c (split(//, $l)) {
-               next if $r !~ /$c/;
-               $sum += ord($c) - ord('A') + 27 if $c =~ /[A-Z]/;
-               $sum += ord($c) - ord('a') +  1 if $c =~ /[a-z]/;
-               say $c;
-               last;
-       }
+       substr($_, length()/2, 0) = ' ';
+       my ($c) = /(.)\S*\s\S*\1/;
+       $sum += $c =~ /[A-Z]/
+               ? ord($c) - ord('A') + 27
+               : ord($c) - ord('a') + 1; 
 }
 say $sum;