1

I'm currently working on Project Euler 18 which involves a triangle of numbers and finding the value of the maximum path from top to bottom. It says you can do this project either by brute forcing it or by figuring out a trick to it. I think I've figured out the trick, but I can't even begin to solve this because I don't know how to start manipulating this triangle in Python.

https://projecteuler.net/problem=18

Here's a smaller example triangle:

   3
  7 4
 2 4 6
8 5 9 3

In this case, the maximum route would be 3 -> 7 -> 4 -> 9 for a value of 23.

Some approaches I considered: I've used NumPy quite a lot for other tasks, so I wondered if an array would work. For that 4 number base triangle, I could maybe do a 4x4 array and fill up the rest with zeros, but aside from not knowing how to import the data in that way, it also doesn't seem very efficient. I also considered a list of lists, where each sublist was a row of the triangle, but I don't know how I'd separate out the terms without going through and adding commas after each term.

Just to emphasise, I'm not looking for a method or a solution to the problem, just a way I can start to manipulate the numbers of the triangle in python.

liam
  • 1,918
  • 3
  • 22
  • 28
Emily
  • 163
  • 1
  • 1
  • 9

1 Answers1

3

Here is a little snippet that should help you with reading the data:

rows = []
with open('problem-18-data') as f:
    for line in f:
        rows.append([int(i) for i in line.rstrip('\n').split(" ")])
liam
  • 1,918
  • 3
  • 22
  • 28