adventofcode/2016/21/1.pl

40 lines
1.0 KiB
Perl
Raw Permalink Normal View History

2018-03-05 18:10:11 +00:00
use 5.20.0;
use List::AllUtils qw/ first_index indexes /;
my @code = split '', 'abcde'; #fgh'; # agcebfdh
while( <> ) {
if( /swap position (\d+) with position (\d+)/ ) {
@code[ $1, $2 ] = @code[$2, $1];
}
elsif( /swap letter (.) with letter (.)/ ) {
my @x = indexes { $_ eq $1 or $_ eq $2 } @code;
@code[ @x ] = @code[ reverse @x ];
}
elsif( /reverse positions (\d+) through (\d+)/ ) {
@code[ $1..$2 ] = reverse @code[ $1..$2 ];
}
elsif( /rotate left (\d+)/ ) {
push @code, shift @code for 1..$1;
}
elsif( /rotate right (\d+)/ ) {
unshift @code, pop @code for 1..$1;
}
elsif( /move position (\d+) to position (\d+)/ ) {
my($x) = splice @code, $1, 1;
splice @code, $2, 0, $x;
}
elsif( /rotate based on position of letter (.)/ ) {
my $i = first_index { $_ eq $1 } @code;
$i++ if $i >= 4;
$i++;
unshift @code, pop @code for 1..$i;
}
else {
die $_;
}
say @code;
}