commit 3b57750fcdb06b3bde6652afa176cbdeb340f15e
parent aaade25cf8ba0eb189d6a55b94ccde4ec00ccbfa
Author: Samir Parikh <noreply@samirparikh.com>
Date: Mon, 7 Nov 2022 22:23:20 +0000
finally solve part 2 of day14!
Diffstat:
2 files changed, 13 insertions(+), 27 deletions(-)
diff --git a/day14/Day14.pm b/day14/Day14.pm
@@ -26,7 +26,7 @@ sub get_position {
my $duration = $fly_duration + $rest_duration;
my $position;
my ( $cycles, $remainder ) = ( int $time / $duration, $time % $duration );
- if ( $remainder >= $fly_duration ) {
+ if ( $remainder > $fly_duration ) {
$cycles++;
$position = $cycles * $fly_duration * $speed;
} else {
diff --git a/day14/day14.pl b/day14/day14.pl
@@ -13,53 +13,39 @@ use Data::Dumper;
chomp( my $input = do { local $/; <> } );
my $TIME = 2503;
-#my @distances;
my %reindeer = %{ init_reindeer( $input ) };
-#foreach ( keys %reindeer ) {
-# push @distances =>
-# get_position( $reindeer{ $_ }{ speed },
-# $reindeer{ $_ }{ fly_duration },
-# $reindeer{ $_ }{ rest_duration },
-# $TIME );
-#}
-#
-#say Dumper \%reindeer;
-
foreach my $time ( 1 .. $TIME ) {
- #say $time;
# find position of each reindeer after each second
- foreach my $reindeer ( keys %reindeer ) {
+ foreach my $reindeer ( sort 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 = '';
+ # find the furthest position
my $furthest_position = 0;
- foreach my $reindeer ( keys %reindeer ) {
+ foreach my $reindeer ( sort 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;
+ # give those reindeer(s) a point
+ foreach my $reindeer ( sort keys %reindeer ) {
+ if ( $reindeer{ $reindeer }{ position } == $furthest_position ) {
+ $reindeer{ $reindeer }{ points } += 1;
+ }
+ }
}
# find distance furthest traveled and most points
-my @distances2;
+my @distances;
my @points;
foreach my $reindeer ( sort keys %reindeer ) {
- push @distances2 => $reindeer{ $reindeer }{ position };
- say "$reindeer got $reindeer{ $reindeer }{ points } points";
+ push @distances => $reindeer{ $reindeer }{ position };
push @points => $reindeer{ $reindeer }{ points };
}
-say "points are @points";
-#say "part 1: ", max @distances;
-say "part 1: ", max @distances2;
+say "part 1: ", max @distances;
say "part 2: ", max @points;