views:

64

answers:

1

Hi

I am doing problem 68 at project euler and came up with the following code in Haskell to return the list of numbers which fit the (given) solution:

lists = [n|n<- permutations [1..6] , ring n ] 
ring [a,b,c,d,e,f] = (length $ nub $ map sum [[d,c,b],[f,b,a],[e,a,c]]) == 1

This only returns a list of lists of 6 numbers each which fit the solution. What I don't know how to do, is make it return the actual solution, the lists that fit the form:

[d,c,b],[f,b,a],[e,a,c]

How can I make lists return a list of this format?

(PS: I will add in the appropriate functions to return what the site actually wants later)

+2  A: 

It's simply

lists = [ [[d,c,b],[f,b,a],[e,a,c]] | n@[a,b,c,d,e,f] <- permutations [1..6], ring n ]

Or in order to generate the strings:

[ foldl (++) "" $ map show [d,c,b,f,b,a,e,a,c] | n@[a,b,c,d,e,f] <- permutations [1..6], ring n ]
Dario
Thanks for the help, I never knew about `@`.
Jonno_FTW
On a further note, I keep getting an incorrect answer of: 432513621 (real answer is: 432621513). I used: `answer = read $ concatMap show $ maximum $ map concat $ map sort lists :: Integer`Care to shed any light on where I am going wrong?
Jonno_FTW