From fe22e05b9db4aa448cf38b22aede84137f8ebee5 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 12 Dec 2023 11:38:01 -0500 Subject: Solve day 6 part 2 --- 2023/day06.pl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/2023/day06.pl b/2023/day06.pl index 92dd798..723ae89 100644 --- a/2023/day06.pl +++ b/2023/day06.pl @@ -6,15 +6,18 @@ while (<>) { @times = split(/ +/) if s/^Time: +//; @dists = split(/ +/) if s/^Distance: +//; } -my $ways = 1; -for (my $race = 0; $race < scalar(@times); $race++) { - my $time = $times[$race]; - my $dist = $dists[$race]; +sub wins { + my ($time, $dist) = @_; my $wins = 0; for my $hold (1 .. $time - 1) { my $travel = $hold * ($time - $hold); $wins++ if $travel > $dist; } - $ways *= $wins; + $wins; +} +my $ways = 1; +for (my $race = 0; $race < scalar(@times); $race++) { + $ways *= wins($times[$race], $dists[$race]); } print $ways, "\n"; +print wins(int(join("", @times)), int(join("", @dists))), "\n"; -- cgit 1.4.1