mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
140 lines
4.3 KiB
Python
140 lines
4.3 KiB
Python
#ProjectEuler/Python/Problem18.py
|
|
#Matthew Ellison
|
|
# Created: 03-12-19
|
|
#Modified: 03-28-19
|
|
#Find the maximum total from top to bottom
|
|
"""
|
|
75
|
|
95 64
|
|
17 47 82
|
|
18 35 87 10
|
|
20 04 82 47 65
|
|
19 01 23 75 03 34
|
|
88 02 77 73 07 63 67
|
|
99 65 04 28 06 16 70 92
|
|
41 41 26 56 83 40 80 70 33
|
|
41 48 72 33 47 32 37 16 94 29
|
|
53 71 44 65 25 43 91 52 97 51 14
|
|
70 11 33 28 77 73 17 78 39 68 17 57
|
|
91 71 52 38 17 14 91 43 58 50 27 29 48
|
|
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
|
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
|
"""
|
|
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
|
|
"""
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
|
|
from Stopwatch import Stopwatch
|
|
from collections import namedtuple
|
|
|
|
|
|
location = namedtuple("location", "xLocation yLocation total fromRight")
|
|
|
|
NUM_ROWS = 15
|
|
|
|
|
|
def invert(listNum):
|
|
for rowCnt in range(0, NUM_ROWS):
|
|
for colCnt in range(0, len(listNum[rowCnt])):
|
|
listNum[rowCnt][colCnt] = 100 - listNum[rowCnt][colCnt]
|
|
|
|
def removeIf(listNum: list, loc):
|
|
location = 0
|
|
while(location < len(listNum)):
|
|
if((listNum[location].xLocation == loc.xLocation) and (listNum[location].yLocation == loc.yLocation)):
|
|
del listNum[location]
|
|
else:
|
|
location += 1
|
|
|
|
def Problem18():
|
|
listNum = [[75],
|
|
[95, 64],
|
|
[17, 47, 82],
|
|
[18, 35, 87, 10],
|
|
[20, 4, 82, 47, 65],
|
|
[19, 1, 23, 75, 3, 34],
|
|
[88, 2, 77, 73, 7, 63, 67],
|
|
[99, 65, 4, 28, 6, 16, 70, 92],
|
|
[41, 41, 26, 56, 83, 40, 80, 70, 33],
|
|
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
|
|
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
|
|
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
|
|
[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
|
|
[63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
|
|
[ 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]
|
|
|
|
#Invert the list so that each element = 100 - element
|
|
invert(listNum)
|
|
|
|
#Holds points we know are of the shortest route
|
|
foundPoints = []
|
|
#Add the tip of the pyramid because everything has to go through that
|
|
foundPoints.append(location(0, 0, listNum[0][0], True))
|
|
#Holds points that might be the shortest route
|
|
possiblePoints = []
|
|
#Add the second row as possible points because everything must pass through the second row
|
|
possiblePoints.append(location(0, 1, (listNum[0][0] + listNum[1][0]), True))
|
|
possiblePoints.append(location(1, 1, (listNum[0][0] + listNum[1][1]), False))
|
|
foundBottom = False
|
|
|
|
#Loop until you find the bottom
|
|
while(not foundBottom):
|
|
#Check which possible point gives us the lowest number. If more than one has the same number simply keep the first one
|
|
minLoc = possiblePoints[0]
|
|
for loc in possiblePoints:
|
|
if(loc.total < minLoc.total):
|
|
minLoc = loc
|
|
|
|
#Remove it from the list of possible points
|
|
removeIf(possiblePoints, minLoc)
|
|
|
|
foundPoints.append(minLoc)
|
|
|
|
#Add to the list of possible points from the point we just found and
|
|
#If you are at the bottom raise the flag to end the program
|
|
xLoc = minLoc.xLocation
|
|
yLoc = minLoc.yLocation + 1
|
|
if(yLoc >= NUM_ROWS):
|
|
foundBottom = True
|
|
else:
|
|
possiblePoints.append(location(xLoc, yLoc, minLoc.total + listNum[yLoc][xLoc], True))
|
|
xLoc += 1
|
|
possiblePoints.append(location(xLoc, yLoc, minLoc.total + listNum[yLoc][xLoc], False))
|
|
|
|
#Get the real total of the journey
|
|
actualTotal = ((100 * NUM_ROWS) - foundPoints[len(foundPoints) - 1].total)
|
|
|
|
#Invert the list so it can be read again
|
|
invert(listNum)
|
|
|
|
#Print the results
|
|
print("The value of the longest path is " + str(actualTotal))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
timer = Stopwatch()
|
|
timer.start()
|
|
Problem18()
|
|
timer.stop()
|
|
print("It took " + timer.getString() + " to run this algorithm")
|
|
|
|
""" Results:
|
|
The value of the longest path is 1074
|
|
It took 654.691 microseconds to run this algorithm
|
|
"""
|