Advertisement
musifter

AoC day 10, Perl, part 2

Dec 10th, 2020
1,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use List::AllUtils  qw(max);
  7.  
  8. no warnings 'recursion';
  9.  
  10. my @memo  = (1);
  11. my @adapt = (0, sort { $a <=> $b } <>);
  12.  
  13. sub recurse_adapt {
  14.     my $idx = shift;
  15.  
  16.     return ($memo[ $idx ])  if (defined $memo[ $idx ]);
  17.  
  18.     my $ret = 0;
  19.     my $min = max( $adapt[$idx] - 3, 0 );
  20.  
  21.     my $i = $idx;
  22.     while( --$i >= 0 && $adapt[$i] >= $min ) {
  23.         $ret += &recurse_adapt( $i );
  24.     }
  25.  
  26.     @memo[$idx] = $ret;
  27.     return( $ret );
  28. }
  29.  
  30. print "Part 2: ", &recurse_adapt( $#adapt ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement