You are reading a single comment by @lf and its replies.
Click here to read the full conversation.
-
Here was my part two, @nos is the array of input integers:-
sub part2 { my ( $n ) = @_; my $s=0; my $e=0; my $t=$nos[$s]; while( $s < scalar( @nos ) ) { if( $t > $n ) { # Running total is too big, remove first number from current window $t-=$nos[$s]; $s++; } if( $t < $n && $e < scalar( @nos )-1 ) { # Running total too small, add another number on the end $e++; $t+=$nos[$e]; } if( $s != $e && $t == $n ) { # Got it, find min and max of current window my $min=$nos[$s]; my $max=$nos[$s]; for my $ct ( $s..$e ) { $min=$nos[$ct] if( $min > $nos[$ct] ); $max=$nos[$ct] if( $max < $nos[$ct] ); } return( $min+$max ); } } }
Jolly good.
Finally redid today's properly (rather than the hack to just get the answers).
Day 9 part 2 induced the usual paranoia that the O(n) sliding window algorithm doesn't work (it does as long as you don't have any negative numbers).