aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

2.pl (661B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 my %vals = (
      6     1 => "1",
      7     2 => "2",
      8     3 => "3",
      9     4 => "4",
     10     5 => "5",
     11     6 => "6",
     12     7 => "7",
     13     8 => "8",
     14     9 => "9",
     15     one => "1",
     16     two => "2",
     17     three => "3",
     18     four => "4",
     19     five => "5",
     20     six => "6",
     21     seven => "7",
     22     eight => "8",
     23     nine => "9",
     24     );
     25 
     26 sub to_int {
     27     my ($n) = @_; 
     28     return $vals{$n};
     29 }
     30 
     31 my $s = 0;
     32 while (<ARGV>) {
     33     $_ =~ /(\d|one|two|three|four|five|six|seven|eight|nine)/;
     34     my $first = $1;
     35     $_ =~ /.*(\d|one|two|three|four|five|six|seven|eight|nine)/;
     36     my $last = $1;
     37     $s += (to_int($first) . to_int($last));
     38 }
     39 
     40 print("$s\n");