#!/usr/bin/perl -w

use strict;
use v5.30;

my $data = 'abcdefgh';
# my $data = 'abcde';

while (<>) {
	chomp;
	say "$data $_";
	if (/swap position (\d+) with position (\d+)/) {
		(substr($data, $1, 1), substr($data, $2, 1))
			= (substr($data, $2, 1), substr($data, $1, 1));
	} elsif (/swap letter (\w) with letter (\w)/) {
		eval "\$data =~ y/$1$2/$2$1/";
	} elsif (/rotate left (\d+) /) {
		my $n = $1;
		$data =~ s/^(.{$n})(.*)/$2$1/;
	} elsif (/rotate right (\d+) /) {
		my $n = $1;
		$data =~ s/^(.*)(.{$n})/$2$1/;
	} elsif (/rotate based on position of letter (\w)/) {
		my $l = $1;
		my ($pref) = $data =~ /^(.*$l)/;
		my $pos = length($pref);
		$pos++ if $pos > 4;
		$pos -= length($data) if $pos >= length($data);
		$data =~ s/^(.*)(.{$pos})/$2$1/ if $pos;
	} elsif (/reverse positions (\d+) through (\d+)/) {
		substr($data, $1, $2-$1+1) = join('', reverse split //,
			substr($data, $1, $2-$1+1));
	} elsif (/move position (\d+) to position (\d+)/) {
		my $l = substr($data, $1, 1);
		substr($data, $1, 1) = '';
		substr($data, $2, 0) = $l;
	} else {
		die "Unknown command $_.";
	}
	
}

say $data;
