From 7a447aea0664be9e46c0c47eccdc65f9315a3444 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sun, 11 Dec 2022 06:20:08 +0100 Subject: [PATCH] Day 11: nice! --- 2022/21.pl | 41 +++++++++++++++++++++++++++++++++++++++++ 2022/22.pl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 2022/21.pl create mode 100755 2022/22.pl diff --git a/2022/21.pl b/2022/21.pl new file mode 100755 index 0000000..6f9921b --- /dev/null +++ b/2022/21.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; + +local $/ = "\n\n"; + +my (@queues, @ops, @divs, @ift, @iff, @steps); + +while (<>) { + my @lines = split /\n/; + push @queues, [ $lines[1] =~ /\d+/g ]; + my ($op) = $lines[2] =~ /= (.*)/; + $op =~ s/old/\$old/g; + push @ops, $op; + push @divs, $lines[3] =~ /(\d+)/; + push @ift, $lines[4] =~ /(\d+)/; + push @iff, $lines[5] =~ /(\d+)/; +} + +for (1 .. 20) { + for my $mk (0 .. $#queues) { + my $q = $queues[$mk]; + while (my $old = shift @$q) { + $steps[$mk]++; + $old = eval $ops[$mk]; + $old = int($old / 3); + if ($old % $divs[$mk]) { + push @{ $queues[$iff[$mk]] }, $old; + } else { + push @{ $queues[$ift[$mk]] }, $old; + } + } + } +} + +@steps = sort { $b <=> $a } @steps; + +say $steps[0] * $steps[1]; + diff --git a/2022/22.pl b/2022/22.pl new file mode 100755 index 0000000..4236c6e --- /dev/null +++ b/2022/22.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; +use List::Util qw(reduce); + +local $/ = "\n\n"; + +my (@queues, @ops, @divs, @ift, @iff, @steps); + +while (<>) { + my @lines = split /\n/; + push @queues, [ $lines[1] =~ /\d+/g ]; + my ($op) = $lines[2] =~ /= (.*)/; + $op =~ s/old/\$old/g; + push @ops, $op; + push @divs, $lines[3] =~ /(\d+)/; + push @ift, $lines[4] =~ /(\d+)/; + push @iff, $lines[5] =~ /(\d+)/; +} + +my $mod = reduce { $a*$b } @divs; + +for (1 .. 10_000) { + for my $mk (0 .. $#queues) { + my $q = $queues[$mk]; + while (my $old = shift @$q) { + $steps[$mk]++; + $old = eval $ops[$mk]; + $old %= $mod; + if ($old % $divs[$mk]) { + push @{ $queues[$iff[$mk]] }, $old; + } else { + push @{ $queues[$ift[$mk]] }, $old; + } + } + } +} + +@steps = sort { $b <=> $a } @steps; + +say $steps[0] * $steps[1]; + -- 2.43.5