This commit is contained in:
Yanick Champoux 2018-03-05 13:10:11 -05:00
parent 62e20d3282
commit d77d0d30fc
4 changed files with 200 additions and 0 deletions

39
2016/21/1.pl Normal file
View File

@ -0,0 +1,39 @@
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;
}

53
2016/21/2.pl Normal file
View File

@ -0,0 +1,53 @@
use 5.20.0;
use List::AllUtils qw/ first_index indexes /;
my @code = split '', 'abcdefgh'; #fgh'; # agcebfdh
my @instructions = <>;
use Algorithm::Combinatorics qw(permutations);
my $comb = permutations(\@code);
while( my $c = $comb->next ) {
my @code = @$c;
my $o = "@code";
for( @instructions ) {
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";
die $o if "@code" eq 'f b g d c e a h';
}

100
2016/21/input.txt Normal file
View File

@ -0,0 +1,100 @@
move position 0 to position 3
rotate right 0 steps
rotate right 1 step
move position 1 to position 5
swap letter h with letter b
reverse positions 1 through 3
swap letter a with letter g
swap letter b with letter h
rotate based on position of letter c
swap letter d with letter c
rotate based on position of letter c
swap position 6 with position 5
rotate right 7 steps
swap letter b with letter h
move position 4 to position 3
swap position 1 with position 0
swap position 7 with position 5
move position 7 to position 1
swap letter c with letter a
move position 7 to position 5
rotate right 4 steps
swap position 0 with position 5
move position 3 to position 1
swap letter c with letter h
rotate based on position of letter d
reverse positions 0 through 2
rotate based on position of letter g
move position 6 to position 7
move position 2 to position 5
swap position 1 with position 0
swap letter f with letter c
rotate right 1 step
reverse positions 2 through 4
rotate left 1 step
rotate based on position of letter h
rotate right 1 step
rotate right 5 steps
swap position 6 with position 3
move position 0 to position 5
swap letter g with letter f
reverse positions 2 through 7
reverse positions 4 through 6
swap position 4 with position 1
move position 2 to position 1
move position 3 to position 1
swap letter b with letter a
rotate based on position of letter b
reverse positions 3 through 5
move position 0 to position 2
rotate based on position of letter b
reverse positions 4 through 5
rotate based on position of letter g
reverse positions 0 through 5
swap letter h with letter c
reverse positions 2 through 5
swap position 7 with position 5
swap letter g with letter d
swap letter d with letter e
move position 1 to position 2
move position 3 to position 2
swap letter d with letter g
swap position 3 with position 7
swap letter b with letter f
rotate right 3 steps
move position 5 to position 3
move position 1 to position 2
rotate based on position of letter b
rotate based on position of letter c
reverse positions 2 through 3
move position 2 to position 3
rotate right 1 step
move position 7 to position 0
rotate right 3 steps
move position 6 to position 3
rotate based on position of letter e
swap letter c with letter b
swap letter f with letter d
swap position 2 with position 5
swap letter f with letter g
rotate based on position of letter a
reverse positions 3 through 4
rotate left 7 steps
rotate left 6 steps
swap letter g with letter b
reverse positions 3 through 6
rotate right 6 steps
rotate based on position of letter c
rotate based on position of letter b
rotate left 1 step
reverse positions 3 through 7
swap letter f with letter g
swap position 4 with position 1
rotate based on position of letter d
move position 0 to position 4
swap position 7 with position 6
rotate right 6 steps
rotate based on position of letter e
move position 7 to position 3
rotate right 3 steps
swap position 1 with position 2

8
2016/21/test Normal file
View File

@ -0,0 +1,8 @@
swap position 4 with position 0
swap letter d with letter b
reverse positions 0 through 4
rotate left 1
move position 1 to position 4
move position 3 to position 0
rotate based on position of letter b
rotate based on position of letter d