| Bytes | Lang | Time | Link |
|---|---|---|---|
| 167 | Python3 | 250312T015840Z | Ajax1234 |
| 120 | Perl | 160828T061747Z | Ton Hosp |
Python3, 167 bytes
def f(N,n,t):
q,s=[(t,0)],[t]
for t,c in q:
if[*range(1,N+1)]==t:return c
for i in range(N-n):
if(T:=t[:i]+t[i:i+n][::-1]+t[i+n:])not in s:q+=[(T,c+1)];s+=[T]
Perl, 120 bytes
Includes +6 for -Xapi (- and space are counted too because both the -i and the $' in the code make it impossible to combine the options with -e)
Run with the input on STDIN and the platform length after the -i option, e.g.
perl -Xapi3 trains.pl <<< "1 4 5 6 3 8 7 2 9"
Prints -2 if there is no solution
trains.pl:
1until$_=$n++*$z{pack"W*",@F}||-!grep!$z{$_}++&&/.{$^I}(??{!\$z{$`.reverse($&).$'}})$/s,keys%z,pack"W*",1..@F;$_--
if N <= 9 then you can gain 5 more bytes:
1until$_=$n++*$z{join"",@F}||-!grep!$z{$_}++&&/.{$^I}(??{!\$z{$`.reverse($&).$'}})$/,keys%z,join"",1..@F;$_--