aoc2015

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

commit 8694dd25c93166844dad98fcb8c17cf519c0632e
parent 2f53d9dadebf206a23075e14f8d913269c56c64a
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon,  7 Nov 2022 14:30:23 +0000

start to refactor day14 solution to put subroutines into separate package file

Diffstat:
Aday14/Day14.pm | 37+++++++++++++++++++++++++++++++++++++
Mday14/day14.pl | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 94 insertions(+), 23 deletions(-)

diff --git a/day14/Day14.pm b/day14/Day14.pm @@ -0,0 +1,37 @@ +package Day14; + +use strict; +use warnings; +use v5.32; +use Exporter qw( import ); + +our @EXPORT = qw( init_reindeer get_position ); + +sub init_reindeer { + my $input = shift; + my %reindeer; + foreach ( split /\n/, $input ) { + my ( $reindeer, $speed, $fly_duration, $rest_duration ) = + m/(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./; + $reindeer{ $reindeer }{ speed } = $speed; + $reindeer{ $reindeer }{ fly_duration } = $fly_duration; + $reindeer{ $reindeer }{ rest_duration } = $rest_duration; + } + return \%reindeer; +} + +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; +} + +1; diff --git a/day14/day14.pl b/day14/day14.pl @@ -4,38 +4,72 @@ use strict; use warnings; use v5.32; +use lib '.'; +use Day14; use List::Util qw( max ); +use Data::Dumper; @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); -my $TIME = 2503; -my %reindeer; + +my $TIME = 2503; my @distances; +my %reindeer = %{ init_reindeer( $input ) }; +say Dumper \%reindeer; -foreach ( split /\n/, $input ) { - my ( $reindeer, $speed, $fly_duration, $rest_duration ) = - m/(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./; - $reindeer{ $reindeer }{ speed } = $speed; - $reindeer{ $reindeer }{ fly_duration } = $fly_duration; - $reindeer{ $reindeer }{ rest_duration } = $rest_duration; -} +#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; +#} +# +# +#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; +#} 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; +# 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 ); } say "part 1: ", max @distances; -#say "part 2: ";