Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::AllUtils qw(max);
- no warnings 'recursion';
- my @memo = (1);
- my @adapt = (0, sort { $a <=> $b } <>);
- sub recurse_adapt {
- my $idx = shift;
- return ($memo[ $idx ]) if (defined $memo[ $idx ]);
- my $ret = 0;
- my $min = max( $adapt[$idx] - 3, 0 );
- my $i = $idx;
- while( --$i >= 0 && $adapt[$i] >= $min ) {
- $ret += &recurse_adapt( $i );
- }
- @memo[$idx] = $ret;
- return( $ret );
- }
- print "Part 2: ", &recurse_adapt( $#adapt ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement