-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP067.py
More file actions
76 lines (62 loc) · 1.93 KB
/
P067.py
File metadata and controls
76 lines (62 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
#==============================================================================
# By starting at the top of the triangle below and moving to adjacent numbers
# on the row below, the maximum total from top to bottom is 23.
#
# 3
# 7 4
# 2 4 6
# 8 5 9 3
#
# That is, 3 + 7 + 4 + 9 = 23.
#
# Find the maximum total from top to bottom in triangle.txt (right click
# and 'Save Link/Target As...'), a 15K text file containing a triangle
# with one-hundred rows.
#
# NOTE: This is a much more difficult version of Problem 18. It is not
# possible to try every route to solve this problem, as there are
# 299 altogether! If you could check one trillion (1012) routes every
# second it would take over twenty billion years to check them all.
# There is an efficient algorithm to solve it. ;o)
#==============================================================================
import numpy as np
#TrigRaw = '''3
#7 4
#2 4 6
#8 5 9 3'''
# Define function to import data files obtained from machine
def readfile(filename):
# Open the file
openfile = open(filename, 'rb')
# for line in openfile:
# print line[:-1].split(" ")
# print 'next'
# CSV reader, comma seperated, keeping second column only
lines = [line[:-1].strip().split(" ") for line in openfile]
Trig = []
for line in lines:
data = []
for item in line:
data.append(int(item))
Trig.append(data)
return Trig
TrigA = readfile('P067tri.txt')
length = len(TrigA)
TrigS = np.zeros([length,length])
ii, jj = 0, 0
for Vector in TrigA:
jj = 00
for Element in Vector:
# print Element, ii, jj
if ii==0:
TrigS[ii][jj] = TrigA[ii][jj]
elif jj==0:
TrigS[ii][jj] = TrigA[ii][jj] + TrigS[ii-1][jj]
else:
TrigS[ii][jj] = TrigA[ii][jj] + max([TrigS[ii-1][jj-1], TrigS[ii-1][jj]])
jj += 1
ii += 1
#print TrigS[-1]
print max(TrigS[-1])
#7273