# Travelling Salesman

## Python 2, ~~237~~ ~~231~~ ~~228~~ 225 characters

Since this is a naive algorithm, its score is probably about 225! ≈ 1.26e433.

```
from itertools import*
a=input()
n=lambda*a:tuple(sorted(a))
d=dict((n(*x[:2]),x[2])for x in a)
print min(permutations(set(chain(*[x[:2]for x in a]))),key=lambda x:sum(d.get(n(x[i],x[i+1]),1e400)for i in range(-1,len(x)-1)))
```

## Haskell, 259

I thought I would be able to get it shorter. maybe I will.

this has time complexity of O(n^2*2^n)* so score is about 6.2e82

_{*i'm not actually sure, but if there is any "addition" to the complexity it's not more than polynomial, so this shouldn't change the score much.}

```
import Data.List
g e=tail$snd$minimum[r|[email protected](_,b)<-iterate(\l->nubBy((.f).(==).f)$sort[(b+d,c:a)|(b,a)<-l,c<-h\\init a,d<-a!!0%c])[(0,[h!!0])]!!length h,b!!0==h!!0]where h=sort$nub$e>>= \(a,b,_)->[a,b];a%b=[z|(x,y,z)<-e,x==a&&y==b||x==b&&y==a]
f(_,x:b)=x:sort b
```

## Julia, 213 chars

Probably goes like `n!n`

, so ~2e407.

```
a=[("New York", "Detroit", 2.2), ("New York", "Dillsburg", 3.7), ("Hong Kong", "Dillsburg", 4), ("Hong Kong", "Detroit", 4), ("Dillsburg", "Detroit", 9000.1), ("New York", "Hong Kong", 9000.01)]
f(a)=(
d(x,y)=(r=filter(z->x in z&&y in z,a);r==[]?Inf:r[1][3]);
m=Inf;
q=0;
c=unique([[z[1] for z=a],[z[2] for z=a]]);
n=length(c);
for p=permutations(c);
x=sum([d(p[i],p[mod1(i+1,n)]) for i=1:n]);
x<m&&(m=x;q=p);
end;
q)
f(a)
```

For readability and to demonstrate use I left in some unscored newlines and tabs as well as example input and a call to the function. Also I used an algorithm that requires `n!`

time, but not `n!`

memory, its slightly more feasible to run.