mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Changed problems to work with a driver function
This commit is contained in:
152
Problems/Problem28.py
Normal file
152
Problems/Problem28.py
Normal file
@@ -0,0 +1,152 @@
|
||||
#ProjectEuler/Python/Problem28.py
|
||||
#Matthew Ellison
|
||||
# Created: 09-22-19
|
||||
#Modified: 07-19-20
|
||||
#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) 2020 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 Problems.Problem import Problem
|
||||
from Stopwatch import Stopwatch
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem28(Problem):
|
||||
#Variables
|
||||
grid = []
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("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")
|
||||
self.sumOfDiagonals = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
#Setup the grid
|
||||
self.grid = self.setupGrid()
|
||||
#Find the sum of the diagonals in the grid
|
||||
self.sumOfDiagonals = self.findSum()
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The sum of the diagonals in the given grid is " + str(self.sumOfDiagonals)
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
#Sets up the grid
|
||||
def setupGrid(self) -> list:
|
||||
#Setup the grid to be the right size and fill it with 0's
|
||||
self.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
|
||||
self.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(self.grid[yLocation][xLocation - 1] != 0):
|
||||
self.grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
yLocation += 1
|
||||
|
||||
#Move left until you reach a blank location above
|
||||
while(self.grid[yLocation - 1][xLocation] != 0):
|
||||
self.grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
xLocation -= 1
|
||||
|
||||
#Move up until you reach a blank location to the right
|
||||
while(self.grid[yLocation][xLocation + 1] != 0):
|
||||
self.grid[yLocation][xLocation] = currentNum
|
||||
currentNum += 1
|
||||
yLocation -= 1
|
||||
|
||||
#Move right until you reach a blank location below
|
||||
while(self.grid[yLocation + 1][xLocation] != 0):
|
||||
self.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(self.grid)):
|
||||
finalLocation = True
|
||||
break
|
||||
return self.grid
|
||||
#Finds the sum of the diagonals in the grid
|
||||
def findSum(self) -> int:
|
||||
sumOfDiagonals = 0
|
||||
leftSide = 0
|
||||
rightSide = len(self.grid) - 1
|
||||
row = 0
|
||||
while(row < len(self.grid)):
|
||||
#This ensure the middle location is only counted once
|
||||
if(leftSide == rightSide):
|
||||
sumOfDiagonals += self.grid[row][leftSide]
|
||||
else:
|
||||
sumOfDiagonals += self.grid[row][leftSide]
|
||||
sumOfDiagonals += self.grid[row][rightSide]
|
||||
row += 1
|
||||
leftSide += 1
|
||||
rightSide -= 1
|
||||
return sumOfDiagonals
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.sumOfDiagonals = 0
|
||||
|
||||
#Gets
|
||||
#Returns the grid
|
||||
def getGrid(self) -> list:
|
||||
return self.grid
|
||||
#Returns the sum of the diagonals
|
||||
def getSum(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the sum")
|
||||
return self.sumOfDiagonals
|
||||
|
||||
|
||||
#This calls the appropriate functions if the script is called stand alone
|
||||
if __name__ == "__main__":
|
||||
problem = Problem28()
|
||||
print(problem.getDescription()) #Print the description
|
||||
problem.solve() #Call the function that answers the problem
|
||||
#Print the results
|
||||
print(problem.getResult())
|
||||
print("It took " + problem.getTime() + " to solve 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