From ceaf4a9778015b8d3779160af22ebc2ca686a320 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 3 Dec 2022 07:02:16 +0100 Subject: [PATCH] Day 3: regexp-based solution --- 2022/05.pl | 14 +++++--------- 2022/06.pl | 21 ++++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/2022/05.pl b/2022/05.pl index 1a78799..a7e1f9d 100755 --- a/2022/05.pl +++ b/2022/05.pl @@ -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; diff --git a/2022/06.pl b/2022/06.pl index 31b1327..84ea0fb 100755 --- a/2022/06.pl +++ b/2022/06.pl @@ -5,16 +5,15 @@ use strict; use experimental 'multidimensional'; my $sum; -chomp (my @bags = <>); - -while (my @a = splice(@bags, 0, 3)) { - for my $c (split(//, $a[0])) { - next if $a[1] !~ /$c/; - next if $a[2] !~ /$c/; - $sum += ord($c) - ord('A') + 27 if $c =~ /[A-Z]/; - $sum += ord($c) - ord('a') + 1 if $c =~ /[a-z]/; - say $c; - last; - } +my $s; +while (<>) { + $s .= $_; + next if $. % 3; + $_ = $s; + $s = ''; + my ($c) = /(.)\S*\s\S*\1\S*\s\S*\1/; + $sum += $c =~ /[A-Z]/ + ? ord($c) - ord('A') + 27 + : ord($c) - ord('a') + 1; } say $sum; -- 2.43.0