Advertisement
musifter

AoC 2023 day 8 part 1 (Perl)

Dec 7th, 2023
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.47 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.26;
  4. use warnings;
  5.  
  6. # Paragraph mode, array of sections
  7. $/ = '';
  8. my @section = map {[split /\n/]} <>;
  9.  
  10. my @dirs = split( //, $section[0][0] );
  11.  
  12. my %graph;
  13. foreach ($section[1]->@*) {
  14.     my ($label, $left, $right) = m#(\w+)#g;
  15.     $graph{$label} = {L => $left, R => $right};
  16. }
  17.  
  18. my $loc   = 'AAA';
  19. my $steps = 0;
  20. while ($loc ne 'ZZZ') {
  21.     $loc = $graph{$loc}{ $dirs[$steps % scalar(@dirs)] };
  22.     $steps++;
  23. }
  24.  
  25. say "Part 1: $steps";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement