aoc2022

Advent of Code 2022 solutions in Perl.
git clone git://git.samirparikh.com/aoc2022
Log | Files | Refs | README

day05.pl (913B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2022-05
      3 # taken from 
      4 # https://github.com/Abigail/AdventOfCode2022/blob/master/Day_05/solution.pl
      5 use strict;
      6 use warnings;
      7 use v5.32;
      8 use Data::Dumper;
      9 
     10 @ARGV = "input" unless @ARGV;
     11 
     12 my @stacks1;
     13 my @stacks2;
     14 while ( <> ) {
     15     last if m/^ 1/;
     16     my $stack_number = 1;
     17     while ( m/(?:   |\[([A-Z])\]) ?/g ) {
     18         # crates at the top of the stack will be at the end of the array
     19         # (e.g. lower crates will be at the beginning of the array)
     20         unshift @{ $stacks1[ $stack_number ] } => $1 if $1;
     21         unshift @{ $stacks2[ $stack_number ] } => $1 if $1;
     22         $stack_number++;
     23     }
     24 }
     25 
     26 <>; # read blank line
     27 
     28 while (<>) {
     29     my ( $move, $from, $to ) = m/\d+/g;
     30     foreach ( 1 .. $move ) {
     31         push @{ $stacks1[ $to ] } => pop @{ $stacks1[ $from ] };
     32     }
     33 }
     34 
     35 
     36 my $part1 = '';
     37 $part1 .= $_->[-1] foreach @stacks1[ 1 .. $#stacks1];
     38 
     39 say "part 1: ", $part1;