musifter

AoC 2025 day 7 part 2 (Perl)

Dec 6th, 2025 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.79 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say state);
  7. use List::AllUtils  qw(firstidx indexes);
  8.  
  9. my @input = map { chomp; [split //] } <>;
  10. my $start = firstidx { $_ eq 'S' } @{shift @input};
  11.  
  12. my @split_table;
  13. while (my $row = shift @input) {
  14.     my %splitters = map {$_ => 1} indexes { $_ ne '.' } @$row;
  15.     next if (!%splitters);
  16.     push( @split_table, \%splitters );
  17. }
  18.  
  19. sub recurse {
  20.     my ($loc, $depth) = @_;
  21.     state %memo;
  22.  
  23.     return (1) if ($depth == @split_table);
  24.  
  25.     $memo{$loc,$depth} //= do {
  26.         if ($split_table[$depth]{$loc}) {
  27.             &recurse( $loc - 1, $depth + 1 ) + &recurse( $loc + 1, $depth + 1 );
  28.         } else {
  29.             &recurse( $loc, $depth + 1 );
  30.         }
  31.     }
  32. }
  33.  
  34. say "Part 2: ", &recurse( $start, 0 );
Advertisement
Add Comment
Please, Sign In to add comment