]> www.fi.muni.cz Git - aoc.git/blob - 2022/10.pl
Day 5: parsing input
[aoc.git] / 2022 / 10.pl
1 #!/usr/bin/perl -w
2
3 use v5.36;
4 use strict;
5 use experimental 'multidimensional';
6
7 my @stacks;
8
9 while (<>) {
10         last if /^ \d/;
11         my $i = 1;
12         my $st = 0;
13         $stacks[$st] //= [];
14         while ($i < length) {
15                 my $c = substr($_, $i, 1);
16                 unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/;
17                 $i += 4;
18                 $st++;
19         }
20 }
21
22 scalar <>;
23
24 while (<>) {
25         my ($amount, $src, $dst) = /(\d+)/g;
26         push @{ $stacks[$dst-1] }, splice @{ $stacks[$src-1] }, -$amount, $amount;
27 }
28
29 for my $st (@stacks) {
30         print $st->[-1];
31 }
32 print "\n";
33