Unlocking the Secrets to a 1-Dimensional Labyrinth
Pyth, 51 bytes
JxQ"^"K-xQ"$"JVQI&}NrG1>JxQrN0=JxQrN0=+K*2t-xQNJ;pK
sum the distance between the door and its key (doubled, to take the round trip), ignoring the "nested" keys and the distance from the start to the end:
JxQ"^" #Initialize the farther point with the starting position
K-xQ"$"J #Initialize the step counter with the difference between the exit and the start
VQ #iterate over the input
I&}NrG1>JxQrN0 #check if is upper and if the keys is father than one stored (to eliminate nested keys)
=JxQrN0 #update the farther key
=+K*2t-xQNJ; #update step counter with the round trip door<>key
pK #print the step counter
same algorythm in python2.7 :
lab=raw_input()
farther_key=lab.index('^')
steps = lab.index('$') - farther_key
for i in lab:
if i.isupper():
if farther_key> lab.index(i.lower()):
farther_key=lab.index(i.lower())
steps+=((lab.index(i) - farther_key)-1)*2
print steps
Python 2, 155 154 134 128 bytes
Edit: Thanks to @user2357112 and @loovjo for their comments that helped me shave another 20 26 bytes off my solution!
def f(l):
i=l.index;b=i('^');t=i('$')-b
for d in filter(str.isupper,l):
k=i(d.lower())
if k<b:b=k;t+=2*(i(d)-k-1)
print t
C, 136 bytes
q,x,p[300],z,c,k;main(i){for(;p[c=getchar()]=++i,c-36;z&&(k+=(x=p[c+32])&&x<q?(q=q>x?x:q,2*i-2*x-1):1))z=p[94],q||(q=z);printf("%d",k);}