The Prime Grid Game
Haskell, 239 characters
p=2:q[3..]
q=filter(#p)
n#(x:y)=n==x||n`mod`x/=0&&(n`div`x<x||n#y)
(n§m)q=n:maybe[](\i->[q-4,q-1,q+1,q+4]>>=(n*10+i)§filter(/=(q,i))m)(lookup q m)
i=[0,1,2,4,5,6,8,9,10]
main=getLine>>=print.maximum.(-1:).q.(i>>=).(0§).zip i.map read.words
Input is given as a single line of nine numbers:
$> echo 1 2 3 3 5 6 1 8 9 | runhaskell 2485-PrimeGrid.hs
81356321
$> echo 1 2 3 4 5 6 7 8 9 | runhaskell 2485-PrimeGrid.hs
69854123
$> echo 1 1 1 1 1 1 1 1 1 | runhaskell 2485-PrimeGrid.hs
11
$> echo 2 2 2 2 2 2 2 2 2 | runhaskell 2485-PrimeGrid.hs
2
$> echo 4 4 4 4 4 4 4 4 4 | runhaskell 2485-PrimeGrid.hs
-1
Python, 286 274 chars
I=lambda:raw_input().split()
m=['']
G=m*4+I()+m+I()+m+I()+m*4
def B(s,p):
d=G[p]
if''==d:return-1
G[p]='';s+=d;n=int(s)
r=max(n if n>1and all(n%i for i in range(2,n**.5+1))else-1,B(s,p-4),B(s,p+4),B(s,p-1),B(s,p+1))
G[p]=d;return r
print max(B('',i)for i in range(15))
This does give a deprecation warning for the float argument to range
. Ignore it, or spend 5 more chars to wrap int()
around it.