Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say state);
- use List::AllUtils qw(firstidx indexes);
- my @input = map { chomp; [split //] } <>;
- my $start = firstidx { $_ eq 'S' } @{shift @input};
- my @split_table;
- while (my $row = shift @input) {
- my %splitters = map {$_ => 1} indexes { $_ ne '.' } @$row;
- next if (!%splitters);
- push( @split_table, \%splitters );
- }
- sub recurse {
- my ($loc, $depth) = @_;
- state %memo;
- return (1) if ($depth == @split_table);
- $memo{$loc,$depth} //= do {
- if ($split_table[$depth]{$loc}) {
- &recurse( $loc - 1, $depth + 1 ) + &recurse( $loc + 1, $depth + 1 );
- } else {
- &recurse( $loc, $depth + 1 );
- }
- }
- }
- say "Part 2: ", &recurse( $start, 0 );
Advertisement
Add Comment
Please, Sign In to add comment