mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with existing files
This commit is contained in:
109
Problem28.py
Normal file
109
Problem28.py
Normal file
@@ -0,0 +1,109 @@
|
||||
#ProjectEuler/Python/Problem28.py
|
||||
#Matthew Ellison
|
||||
# Created: 09-22-19
|
||||
#Modified: 09-22-19
|
||||
#What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
|
||||
#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
|
||||
|
||||
|
||||
def setupGrid() -> list:
|
||||
#Setup the grid to be the right size and fill it with 0's
|
||||
grid = [[0 for x in range(1001)] for y in range(1001)]
|
||||
|
||||
finalLocation = False #A flag to indicate if the final location to be filled has been reached
|
||||
currentNum = 1 #Set the number that is going to be put at each location
|
||||
#Start with the middle location and set it correctly and advance the tracker to the next number
|
||||
xLocation = 500
|
||||
yLocation = 500
|
||||
grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
#Move right the first time
|
||||
xLocation += 1
|
||||
#Move in a circular pattern until you reach the final location
|
||||
while(not finalLocation):
|
||||
#Move down until you reach a blank location on the left
|
||||
while(grid[yLocation][xLocation - 1] != 0):
|
||||
grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
yLocation += 1
|
||||
|
||||
#Move left until you reach a blank location above
|
||||
while(grid[yLocation - 1][xLocation] != 0):
|
||||
grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
xLocation -= 1
|
||||
|
||||
#Move up until you reach a blank location to the right
|
||||
while(grid[yLocation][xLocation + 1] != 0):
|
||||
grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
yLocation -= 1
|
||||
|
||||
#Move right until you reach a blank location below
|
||||
while(grid[yLocation + 1][xLocation] != 0):
|
||||
grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
xLocation += 1
|
||||
#Check if you are at the final location and break the loop if you are
|
||||
if(xLocation == len(grid)):
|
||||
finalLocation = True
|
||||
break
|
||||
return grid
|
||||
|
||||
def findSum(grid: list) -> int:
|
||||
sumOfDiagonals = 0
|
||||
leftSide = 0
|
||||
rightSide = len(grid) - 1
|
||||
row = 0
|
||||
while(row < len(grid)):
|
||||
#This ensure the middle location is only counted once
|
||||
if(leftSide == rightSide):
|
||||
sumOfDiagonals += grid[row][leftSide]
|
||||
else:
|
||||
sumOfDiagonals += grid[row][leftSide]
|
||||
sumOfDiagonals += grid[row][rightSide]
|
||||
row += 1
|
||||
leftSide += 1
|
||||
rightSide -= 1
|
||||
return sumOfDiagonals
|
||||
|
||||
def Problem28():
|
||||
#Setup the grid
|
||||
grid = setupGrid()
|
||||
#Find the sum of the diagonals in the grid
|
||||
diagSum = findSum(grid)
|
||||
|
||||
#Print the results
|
||||
print("The sum of the diagonals in the given grid is " + str(diagSum))
|
||||
|
||||
#This calls the appropriate functions if the script is called stand alone
|
||||
if __name__ == "__main__":
|
||||
timer = Stopwatch()
|
||||
timer.start()
|
||||
Problem28()
|
||||
timer.stop()
|
||||
print("It took " + timer.getString() + " to run this algorithm")
|
||||
|
||||
""" Results:
|
||||
The sum of the diagonals in the given grid is 669171001
|
||||
It took 197.764 milliseconds to run this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user