From 7ef41e9dc4681907ba1f59c01982e598a6e4419d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 1 Dec 2023 07:04:17 +0100 Subject: [PATCH] 2023 day 1 part 2: prettier solution --- 2023/02.pl | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) 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."; } -- 2.43.5