#!/usr/local/bin/perl # day 2022-05 # taken from # https://github.com/Abigail/AdventOfCode2022/blob/master/Day_05/solution.pl use strict; use warnings; use v5.32; use Data::Dumper; @ARGV = "input" unless @ARGV; my @stacks1; my @stacks2; while ( <> ) { last if m/^ 1/; my $stack_number = 1; while ( m/(?: |\[([A-Z])\]) ?/g ) { # crates at the top of the stack will be at the end of the array # (e.g. lower crates will be at the beginning of the array) unshift @{ $stacks1[ $stack_number ] } => $1 if $1; unshift @{ $stacks2[ $stack_number ] } => $1 if $1; $stack_number++; } } <>; # read blank line while (<>) { my ( $move, $from, $to ) = m/\d+/g; foreach ( 1 .. $move ) { push @{ $stacks1[ $to ] } => pop @{ $stacks1[ $from ] }; } } my $part1 = ''; $part1 .= $_->[-1] foreach @stacks1[ 1 .. $#stacks1]; say "part 1: ", $part1;