Monday, 29 June 2015

The Fifth Problem (4)

Right, so now I can built the solution.  First get all those combinations of  the operations:

let ops = take 6561 (iterate cyclops "________")

Then map mergeops onto all of those to make a list of strings like "1_2_3+4_5_6-7_8_9" etc:

let rawStrings = map (mergeops "123456789") ops

Then use that little filter to remove the underline characters from the strings:

let neatStrings = map (filter (\c -> c /= '_')) rawStrings

Then just extract the ones where the string evaluates to 100:

let hundreds = filter (\s -> eval s == 100) neatStrings

At this point you might be thinking, blimey, Haskell is fast.  Actually of course due to the magic of lazy evaluation Haskell hasn't done anything yet.  Now we actually ask to see the results and the cogs have to turn:

*Main Data.Char> hundreds

Now I see how that goes I could make a tidy logical version ("Plan to throw one away")

Looking at the solution posted by the blogger I see he has gone for a neat recursive way to deduce the answers.  Er or I mean neat in concept: I can't work out what the actual code does.  I must admit on this one it never crossed my mind not to use the brute force approach.  Logically I should now try to code up Santiago's solution in Haskell.  Hmm.

No comments:

Post a Comment