logo

adventofcode

Code used to solve https://adventofcode.com/, one branch per year git clone https://hacktivis.me/git/adventofcode.git
commit: 409f7d633f7a109e87a2b32b7fcec2cec5acda0e
parent db4fb712bffff95580d5270d6123544dca4624d2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  2 Dec 2023 14:10:35 +0100

day1_part2.pl: new

Diffstat:

Aday1_part2.pl39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/day1_part2.pl b/day1_part2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +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";