Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.32;
- use warnings;
- $/ = '';
- # Snarf sections
- my @section = map {[split /\n/]} <>;
- # grab last line of first section and build (pos -> name) key map for the stacks
- my %key;
- $_ = pop( $section[0]->@* );
- $key{pos() - 1} = $1 while (m#(\w)#g);
- # Use rest of section one to build initial stacks
- my %stack;
- foreach my $line ($section[0]->@*) {
- # Check each position for a crate and stack if found
- foreach my $pos (keys %key) {
- my $crate = substr( $line, $pos, 1 );
- push( $stack{$key{$pos}}->@*, $crate ) if ($crate ne ' ');
- }
- }
- # Execute commands from second section
- foreach my $cmd ($section[1]->@*) {
- my ($num, $src, $dst) = ($cmd =~ m#move (\d+) from (.) to (.)#);
- # Remove reverse for part two:
- unshift( $stack{$dst}->@*, reverse splice( $stack{$src}->@*, 0, $num ) );
- }
- # First crate from the stacks in the order keys were listed:
- say "Part 1: ", map { $stack{$key{$_}}[0] } sort {$a <=> $b} keys %key;
Advertisement
Comments
-
- Just part 1 here. For part 2 you just need to remove the word "reverse".
Add Comment
Please, Sign In to add comment
Advertisement