Skip to content

Problem 18

March 13, 2010

In order to solve the 18th Euler problem, I used a lookahead function that tries to find out which position to choose in the current level of the triangle based on the next level. It also uses the last position used in order to pick the right values in the next levels of the triangle.

First, I defined both triangles using a list of lists, as one can see below.

import List

type Triag = [[Int]]

triangle :: Triag
triangle =

triangleExample :: Triag
triangleExample =

Then, I created a few utility functions that I thought I could use, such as maxValue, which returns the position of the best number among 2; the indexOf, which btw I am not very proud of, that returns the position of a given number in a list (note: although one should never define such a function like that, I am sure I will find an element in that list, so the result will always be correct); and finally maxOf, which tells the lookahead function which position it should return based on the next level of the triangle.

Here are the definitions:

maxValue ::
  Int   -> --position
  [Int] -> --list

maxValue p l =
 let a = (!!) l p
     b = (!!) l (p+1)
 in case a >= b of
     True  -> p
     False -> p+1

indexOf ::
  Int   -> --element
  [Int] -> --list
  Int      --position in the list
indexOf n l =
   case (elemIndex n l) of
   Nothing -> 0
   Just x -> x

maxOf l = let k = unzip l
              m = (maximum . fst) k
              n = indexOf m (fst k)
          in (!!) (snd k) n

The lookahead calculates the sum of the relevant values in the current and next level of the triangle, and returns the best position to choose:

lookahead ::
  [Int] ->  -- Line 1
  [Int] ->  -- Line 2
  Int   ->  -- Column value
  Int       -- Column position where to go next

lookahead l1 l2 c =
  let x1_l1 = (!!) l1 c
      x2_l1 = (!!) l1 (c+1)
      v1    = (x1_l1 + (!!) l2 c,c)
      v2    = (x1_l1 + (!!) l2 (c+1),c)
      v3    = (x2_l1 + (!!) l2 (c+1),c+1)
      v4    = (x2_l1 + (!!) l2 (c+2),c+1)
      l     = [v1,v2,v3,v4]
  in maxOf l

Finally, it was a matter of assembling everything:

total_max ::
  Triag -> -- Triangle
  Int   -> -- Next column
  Int      -- maximum total

total_max (l1:l2:l3:ls) n =
  let next = lookahead l2 l3 n
      val  = (!!) l1 n
  in val + total_max (l2:l3:ls) next
total_max (l1:l2:[]) n =
  let val = (!!) l1 n
      nextval = maxValue n l2
  in val + (!!) l2 nextval

total = total_max triangle 0

The interpreter returned the following output, when I executed the total function:

(0.03 secs, 4125124 bytes)

Although it is not a pretty solution, it solved the problem relatively fast. Now I will try it in problem 67, where there are 299 possible routes!


From → Project Euler

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: