Skip to content

Commit

Permalink
Added Devel::Timer, because I'm curious
Browse files Browse the repository at this point in the history
✗ time perl day7_part2.pl < day7.txt
Move to 464, which will cost you 98363777

Devel::Timer Report -- Total time: 0.3363 secs
Interval  Time    Percent
----------------------------------------------
01 -> 02  0.3356  99.81%  input parsing complete -> solution found
00 -> 01  0.0006   0.19%  INIT -> input parsing complete
perl day7_part2.pl < day7.txt  0.35s user 0.01s system 99% cpu 0.361 total
  • Loading branch information
jhannah committed Dec 7, 2021
1 parent c481309 commit eef9142
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 2021/day7_part2.pl
@@ -1,7 +1,9 @@
use Modern::Perl;
use ARGV::OrDATA;
use List::Util qw(sum);
use Devel::Timer;

my $t = Devel::Timer->new();
my %pos; # count of crabs at each position
while (<>) {
chomp;
Expand All @@ -13,14 +15,17 @@
my @all_pos = sort { $a <=> $b } keys %pos;
my $min = $all_pos[0];
my $max = $all_pos[-1];
$t->mark('input parsing complete');

my %all_costs;
foreach my $x ($min..$max) {
my $cost = cost_to_move_to($x);
$all_costs{$cost} = $x;
}
my ($min_cost) = sort { $a <=> $b } keys %all_costs;
say "Move to $all_costs{$min_cost}, which will cost you $min_cost";

$t->mark('solution found');
$t->report;

sub cost_to_move_to {
(my $x) = @_;
Expand Down

0 comments on commit eef9142

Please sign in to comment.