aoc2015

Advent of Code 2015 solutions in Perl.
git clone git://git.samirparikh.com/aoc2015
Log | Files | Refs | README

day14.pl (1221B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2015-14
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 use lib '.';
      8 use Day14;
      9 use List::Util qw( max );
     10 
     11 @ARGV = "input" unless @ARGV;
     12 chomp( my $input = do { local $/; <> } );
     13 
     14 my $TIME = 2503; 
     15 my %reindeer = %{ init_reindeer( $input ) };
     16 
     17 foreach my $time ( 1 .. $TIME ) {
     18     # after each second...
     19     # ...find position of each reindeer...
     20     $reindeer{ $_ }{ position } = get_position( $reindeer{ $_ }{ speed },
     21                                                 $reindeer{ $_ }{ fly_duration },
     22                                                 $reindeer{ $_ }{ rest_duration },
     23                                                 $time ) foreach ( keys %reindeer );
     24     # ...and the furthest distance traveled after that second... 
     25     my $furthest_position = max map { $reindeer{ $_ }{ position }} keys %reindeer;
     26     # ...by which reindeer(s)...
     27     my @furthest_reindeer =
     28         grep { $reindeer{ $_ }{ position } == $furthest_position } keys %reindeer;
     29     # ...and give them a point
     30     $reindeer{ $_ }{ points } += 1 foreach @furthest_reindeer;
     31 }
     32 
     33 say "part 1: ", max map { $reindeer{ $_ }{ position } } keys %reindeer;
     34 say "part 2: ", max map { $reindeer{ $_ }{ points } }   keys %reindeer;