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.";
}