From: Jan "Yenya" Kasprzak Date: Fri, 1 Dec 2023 06:04:17 +0000 (+0100) Subject: 2023 day 1 part 2: prettier solution X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=7ef41e9dc4681907ba1f59c01982e598a6e4419d;p=aoc.git 2023 day 1 part 2: prettier solution --- 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."; }