From: Jan "Yenya" Kasprzak Date: Mon, 5 Dec 2022 05:16:48 +0000 (+0100) Subject: Day 5: parsing input X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc.git;a=commitdiff_plain;h=992ea7b162b0ce64553e4c10118d12163b13c696 Day 5: parsing input --- diff --git a/2022/09.pl b/2022/09.pl new file mode 100755 index 0000000..ef96b52 --- /dev/null +++ b/2022/09.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; + +my @stacks; + +while (<>) { + last if /^ \d/; + my $i = 1; + my $st = 0; + $stacks[$st] //= []; + while ($i < length) { + my $c = substr($_, $i, 1); + unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/; + $i += 4; + $st++; + } +} + +scalar <>; + +while (<>) { + my ($amount, $src, $dst) = /(\d+)/g; + push @{ $stacks[$dst-1] }, pop @{ $stacks[$src-1] } for 1 .. $amount; +} + +for my $st (@stacks) { + print $st->[-1]; +} +print "\n"; + diff --git a/2022/10.pl b/2022/10.pl new file mode 100755 index 0000000..913e078 --- /dev/null +++ b/2022/10.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; + +my @stacks; + +while (<>) { + last if /^ \d/; + my $i = 1; + my $st = 0; + $stacks[$st] //= []; + while ($i < length) { + my $c = substr($_, $i, 1); + unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/; + $i += 4; + $st++; + } +} + +scalar <>; + +while (<>) { + my ($amount, $src, $dst) = /(\d+)/g; + push @{ $stacks[$dst-1] }, splice @{ $stacks[$src-1] }, -$amount, $amount; +} + +for my $st (@stacks) { + print $st->[-1]; +} +print "\n"; +