From abca4967c0cb8b9e23df1a1a490566aa9a13440d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 14 Dec 2021 06:55:40 +0100 Subject: [PATCH] Day 14: too slow. And no regexes :-( --- 27.pl | 36 ++++++++++++++++++++++++++++++++++++ 28.pl | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 27.pl create mode 100755 28.pl diff --git a/27.pl b/27.pl new file mode 100755 index 0000000..d07c3ae --- /dev/null +++ b/27.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +use v5.16; + +chomp (my $tmpl = <>); +scalar <>; + +my %r; +while (<>) { + chomp; + my ($s1, $s2, $d) = /([A-Z])/g; + $r{"$s1$s2"} = "$s1$d$s2"; +} + +for (1 .. 10) { + my $i = 0; + while ($i < length $tmpl) { + my $s = substr($tmpl, $i, 2); + if ($r{$s}) { + substr($tmpl, $i, 2) = $r{$s}; + $i+=2; + next; + } + $i++; + } +} + +my %count; +for ('A' .. 'Z') { + $count{$_} = () = $tmpl =~ /$_/g; +} + +my @count = sort { $a <=> $b } grep { $_ > 0 } values %count; + +say $count[-1] - $count[0]; + diff --git a/28.pl b/28.pl new file mode 100755 index 0000000..5717759 --- /dev/null +++ b/28.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +use v5.16; +use List::Util qw(sum0); + +chomp (my $tmpl = <>); +scalar <>; + +my (%r1, %r2, %ins); +while (<>) { + chomp; + my ($s1, $s2, $d) = /([A-Z])/g; + $r1{"$s1$s2"} = "$s1$d"; + $r2{"$s1$s2"} = "$d$s2"; + $ins{"$s1$s2"} = $d; +} + +my %count; +my %p; +for my $i (0 .. length($tmpl) - 2) { + $p{substr($tmpl, $i, 2)}++; + $count{substr($tmpl, $i, 1)}++; +} +$count{substr($tmpl, -1, 1)}++; + +for (1 .. 40) { + my %p1; + for my $pair (keys %p) { + if ($r1{$pair}) { + $p1{$r1{$pair}} += $p{$pair}; + $p1{$r2{$pair}} += $p{$pair}; + $count{$ins{$pair}} += $p{$pair}; + } + } + %p = %p1; +} + +my @count = sort { $a <=> $b } values %count; + +say $count[-1] - $count[0]; + -- 2.43.5