X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=2023%2F02.pl;h=b4701ca92119e125d41f69f55fdf2675163afee5;hb=7ef41e9dc4681907ba1f59c01982e598a6e4419d;hp=56e0c2696be023e797acb4a7f0f1b11b8fe91acc;hpb=9267829e6732c6950ece1b335d6554cd511e6e3f;p=aoc.git diff --git a/2023/02.pl b/2023/02.pl index 56e0c26..b4701ca 100755 --- a/2023/02.pl +++ b/2023/02.pl @@ -3,26 +3,19 @@ use v5.38; use strict; use experimental 'multidimensional', 'for_list', 'builtin'; +use builtin 'indexed'; my $sum; - -my %map = ( - one => 1, - two => 2, - three => 3, - four => 4, - five => 5, - six => 6, - seven => 7, - eight => 8, - nine => 9, -); +my @digits = qw(zero one two three four five six seven eight nine); +my %map = reverse indexed @digits; +my $re = join('|', '\d', @digits); while (<>) { - my ($first) = /(\d|one|two|three|four|five|six|seven|eight|nine)/; - my ($last) = /(?:.*)(\d|one|two|three|four|five|six|seven|eight|nine)/; - $first = $map{$first} if exists $map{$first}; - $last = $map{$last} if exists $map{$last}; + my ($first) = /($re)/; + my ($last) = /.*($re)/; + for ($first, $last) { + $_ = $map{$_} if !/\d/; + } $sum += "$first$last"; # say "$_ $first $last."; }