aoc2015

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

commit aaade25cf8ba0eb189d6a55b94ccde4ec00ccbfa
parent 8694dd25c93166844dad98fcb8c17cf519c0632e
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon,  7 Nov 2022 18:47:21 +0000

make updates but yet to solve part 2

Diffstat:
Mday14/Day14.pm | 1+
Mday14/day14.pl | 92+++++++++++++++++++++++++++++++++++--------------------------------------------
2 files changed, 42 insertions(+), 51 deletions(-)

diff --git a/day14/Day14.pm b/day14/Day14.pm @@ -16,6 +16,7 @@ sub init_reindeer { $reindeer{ $reindeer }{ speed } = $speed; $reindeer{ $reindeer }{ fly_duration } = $fly_duration; $reindeer{ $reindeer }{ rest_duration } = $rest_duration; + $reindeer{ $reindeer }{ points } = 0; } return \%reindeer; } diff --git a/day14/day14.pl b/day14/day14.pl @@ -13,63 +13,53 @@ use Data::Dumper; chomp( my $input = do { local $/; <> } ); my $TIME = 2503; -my @distances; +#my @distances; my %reindeer = %{ init_reindeer( $input ) }; -say Dumper \%reindeer; #foreach ( keys %reindeer ) { -# my $duration = $reindeer{ $_ }{ fly_duration } + -# $reindeer{ $_ }{ rest_duration }; -# my $distance; -# my ( $cycles, $remainder ) = ( int $TIME / $duration, $TIME % $duration ); -# if ( $remainder >= $reindeer{ $_ }{ fly_duration } ) { -# $cycles++; -# $distance = $cycles * $reindeer{ $_ }{ fly_duration } * -# $reindeer{ $_ }{ speed }; -# } else { -# $distance = $cycles * $reindeer{ $_ }{ fly_duration } + -# ( $remainder * $reindeer{ $_ }{ speed } ); -# } -# push @distances => $distance; +# push @distances => +# get_position( $reindeer{ $_ }{ speed }, +# $reindeer{ $_ }{ fly_duration }, +# $reindeer{ $_ }{ rest_duration }, +# $TIME ); #} # -# -#say "part 1: ", max @distances; -#say "part 2: "; - -#sub get_position { -# my ( $speed, $fly_duration, $rest_duration, $time ) = @_; -# my $duration = $fly_duration + $rest_duration; -# my $position; -# my ( $cycles, $remainder ) = ( int $time / $duration, $time % $duration ); -# if ( $remainder >= $fly_duration ) { -# $cycles++; -# $position = $cycles * $fly_duration * $speed; -# } else { -# $position = $cycles * $fly_duration * $speed + $remainder * $speed; -# } -# return $position; -#} +#say Dumper \%reindeer; -foreach ( keys %reindeer ) { -# my $duration = $reindeer{ $_ }{ fly_duration } + -# $reindeer{ $_ }{ rest_duration }; -# my $distance; -# my ( $cycles, $remainder ) = ( int $TIME / $duration, $TIME % $duration ); -# if ( $remainder >= $reindeer{ $_ }{ fly_duration } ) { -# $cycles++; -# $distance = $cycles * $reindeer{ $_ }{ fly_duration } * -# $reindeer{ $_ }{ speed }; -# } else { -# $distance = $cycles * $reindeer{ $_ }{ fly_duration } + -# ( $remainder * $reindeer{ $_ }{ speed } ); -# } - push @distances => - get_position( $reindeer{ $_ }{ speed }, - $reindeer{ $_ }{ fly_duration }, - $reindeer{ $_ }{ rest_duration }, - $TIME ); +foreach my $time ( 1 .. $TIME ) { + #say $time; + # find position of each reindeer after each second + foreach my $reindeer ( keys %reindeer ) { + $reindeer{ $reindeer }{ position } = + get_position( $reindeer{ $reindeer }{ speed }, + $reindeer{ $reindeer }{ fly_duration }, + $reindeer{ $reindeer }{ rest_duration }, + $time ); + } + # find which reindeer has furthest position + my $current_winner = ''; + my $furthest_position = 0; + foreach my $reindeer ( keys %reindeer ) { + if ( $reindeer{ $reindeer }{ position } > $furthest_position ) { + $furthest_position = $reindeer{ $reindeer }{ position }; + $current_winner = $reindeer; + } + #say "at time $time, $reindeer is at $reindeer{ $reindeer }{ position }"; + } + # give that reindeer a point + $reindeer{ $current_winner }{ points } += 1; } +# find distance furthest traveled and most points +my @distances2; +my @points; -say "part 1: ", max @distances; +foreach my $reindeer ( sort keys %reindeer ) { + push @distances2 => $reindeer{ $reindeer }{ position }; + say "$reindeer got $reindeer{ $reindeer }{ points } points"; + push @points => $reindeer{ $reindeer }{ points }; +} +say "points are @points"; +#say "part 1: ", max @distances; +say "part 1: ", max @distances2; +say "part 2: ", max @points;