day 1
part 1
perl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
my $total = 0;
for my $line (<>) {
my @nums = ($line =~ /\d/g);
$total += $nums[0] * 10 + $nums[-1];
}
say $total;
part 2
perl
#!/usr/bin/env perl
use strict;
use warnings;
use v5.010;
my %nums = (one => 1, two => 2, three => 3, four => 4, five => 5, six => 6, seven => 7, eight => 8, nine => 9);
$nums{$_} = $_ for 1..9;
my $regex = join "|", keys %nums;
my $total = 0;
for my $line (<>) {
$line =~ /($regex)/;
my $first_num = $nums{$1};
my $window = 1;
my $sub = substr $line, -1;
while ($sub !~ /($regex)/) {
$window ++;
$sub = substr $line, -$window;
}
$sub =~ /($regex)/;
my $second_num = $nums{$1};
$total += $first_num * 10 + $second_num;
}
say $total;
Part 2 gave me a surprising amount of trouble. I resolved it by looking at longer and longer substrings from the end of the line in order to find the very last word even if it overlapped, which you can't do with normal regex split. I doubt this is the most efficient possible solution.
Also Lemmy is eating my < characters inside code blocks, which seems wrong. Pretend the "<>" part says "<>", lol
day 2
perl
Found this much easier than day 1 honestly...