]> www.fi.muni.cz Git - aoc.git/blob - 2016/41.pl
The rest of Year 2016
[aoc.git] / 2016 / 41.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use v5.30;
5
6 my $data = 'abcdefgh';
7 # my $data = 'abcde';
8
9 while (<>) {
10         chomp;
11         say "$data $_";
12         if (/swap position (\d+) with position (\d+)/) {
13                 (substr($data, $1, 1), substr($data, $2, 1))
14                         = (substr($data, $2, 1), substr($data, $1, 1));
15         } elsif (/swap letter (\w) with letter (\w)/) {
16                 eval "\$data =~ y/$1$2/$2$1/";
17         } elsif (/rotate left (\d+) /) {
18                 my $n = $1;
19                 $data =~ s/^(.{$n})(.*)/$2$1/;
20         } elsif (/rotate right (\d+) /) {
21                 my $n = $1;
22                 $data =~ s/^(.*)(.{$n})/$2$1/;
23         } elsif (/rotate based on position of letter (\w)/) {
24                 my $l = $1;
25                 my ($pref) = $data =~ /^(.*$l)/;
26                 my $pos = length($pref);
27                 $pos++ if $pos > 4;
28                 $pos -= length($data) if $pos >= length($data);
29                 $data =~ s/^(.*)(.{$pos})/$2$1/ if $pos;
30         } elsif (/reverse positions (\d+) through (\d+)/) {
31                 substr($data, $1, $2-$1+1) = join('', reverse split //,
32                         substr($data, $1, $2-$1+1));
33         } elsif (/move position (\d+) to position (\d+)/) {
34                 my $l = substr($data, $1, 1);
35                 substr($data, $1, 1) = '';
36                 substr($data, $2, 0) = $l;
37         } else {
38                 die "Unknown command $_.";
39         }
40         
41 }
42
43 say $data;