Files
ProjectEulerPython/Problem15.py

67 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ProjectEuler/Python/Problem15.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 03-28-19
#How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
#There must be a better way than this. This is untested. I let it run for about 14 hours and it still hadn't spit an answer out for me
#But it is programed exactly as I programmed the C++ solution, so the eventual answer should be correct
"""
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
__gridWidth = 20 #The height of the grid
__gridHeight = 20 #The width of the grid
def movement(currentX: int, currentY: int) -> int:
#Return 1 if you are at the finish location
if((currentX == __gridWidth) and (currentY == __gridHeight)):
return 1
numberMoves = 0
#Otherwise move one right if you can and recurse
if(currentX < __gridWidth):
numberMoves = movement(currentX + 1, currentY)
#Then move one down and recurse
if(currentY < __gridHeight):
numberMoves += movement(currentX, currentY + 1)
return numberMoves
def Problem15():
#Start the recursion at the right location and catch what is returned
numberMoves = movement(0, 0)
#Print the results
print("The number of paths from 1 corner of a " + str(__gridWidth) + " x " + str(__gridHeight) + " grid to the opposite corner is " + str(numberMoves))
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
timer = Stopwatch() #Determines the algorithm's run time
timer.start() #Start the timer
Problem15() #Call the function that answers the question
timer.stop() #Stop the timer
#Print the results of the timer
print("It took " + timer.getString() + " to run this algorithm")
"""Results:
"""