Advertisement
musifter

AoC day 16, part 1 (Perl)

Dec 16th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.35 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. use Array::Heap::PriorityQueue::Numeric;
  9. use Math::Vector::Real;
  10.  
  11. my ($vy,$vx) = Math::Vector::Real->canonical_base(2);
  12. my @Dirs = ($vx, -$vy, -$vx, $vy);
  13.  
  14. my @Grid = map { chomp; [split(//)] } <>;
  15.  
  16. sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
  17. sub print_grid  { say "\t", join( '', @$_ ) foreach (@Grid); }
  18.  
  19. my ($start, $end);
  20. foreach my $y (0 .. $#Grid) {
  21.     foreach my $x (0 .. $Grid[0]->$#*) {
  22.         $start = V($x,$y)  if ($Grid[$x][$y] eq 'S');
  23.         $end   = V($x,$y)  if ($Grid[$x][$y] eq 'E');
  24.     }
  25. }
  26.  
  27. my %visit;
  28. my $queue = new Array::Heap::PriorityQueue::Numeric;
  29.  
  30. $queue->add( [$start, 0, 0, 0], 0 );
  31.  
  32. QUEUE:
  33. while (my $state = $queue->get) {
  34.     my ($pos, $face, $turn, $moves) = @$state;
  35.  
  36.     if (grid_at($pos) eq 'E') {
  37.         say "Part 1: $moves";
  38.         last QUEUE;
  39.     }
  40.  
  41.     next QUEUE  if (exists $visit{$pos,$face});
  42.     $visit{$pos,$face} = $moves;
  43.  
  44.     # Try straight if valid
  45.     if (grid_at( $pos + $Dirs[$face] ) ne '#') {
  46.         $queue->add( [$pos + $Dirs[$face], $face, 0, $moves + 1], $moves + 1);
  47.     }
  48.  
  49.     # Try turns (if we didn't just turn the other way)
  50.     foreach my $t (grep {$turn != -$_} (1,-1)) {
  51.         $queue->add( [$pos, ($face+$t) % 4, $t, $moves + 1000], $moves + 1000);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement