Advertisement
musifter

AoC 2023, day 1, part 2 (Perl)

Dec 1st, 2023
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.52 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.16;
  4. use warnings;
  5.  
  6. my %table = ('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
  7.              'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9,
  8.              map { $_ => $_ } (1 .. 9));
  9.  
  10. my $part2 = 0;
  11. foreach (<>) {
  12.     # first match
  13.     m#(one|two|three|four|five|six|seven|eight|nine|\d)#;
  14.     $part2 += 10 * $table{$1};
  15.  
  16.     # match with longest .* before
  17.     m#^.*(one|two|three|four|five|six|seven|eight|nine|\d)#;
  18.     $part2 += $table{$1};
  19. }
  20.  
  21. say "Part 2: $part2";
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement