day1_part2.pl (821B)
- #!/usr/bin/env perl
- # SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+aoc2023@hacktivis.me>
- # SPDX-License-Identifier: MIT
- use strict;
- my $base = qr<(one|two|three|four|five|six|seven|six|seven|eight|nine|[0-9])>;
- sub traa {
- ($_) = @_;
- $_ =~ s/one/1/;
- $_ =~ s/two/2/;
- $_ =~ s/three/3/;
- $_ =~ s/four/4/;
- $_ =~ s/five/5/;
- $_ =~ s/six/6/;
- $_ =~ s/seven/7/;
- $_ =~ s/eight/8/;
- $_ =~ s/nine/9/;
- return $_;
- }
- my $total = 0;
- while(my $line = <>) {
- chomp( $line );
- print "$line ";
- # .*? for non-greedy / smallest matching
- my $first = traa($line =~ qr<^.*?${base}>);
- # For some reason it needs .* at the start
- my $last = traa($line =~ qr<.*${base}.*?$>);
- print "($first | $last)";
- my $val = int($first.$last);
- print "= ", $val, "\n";
- $total += $val;
- }
- print "Total: ", $total, "\n";