#Grid is a 2d array of elements def findLargest(grid): length = len(grid[0]) height = len(grid) #Make sure all rows are equal in size for row in grid: if len(row) != length: print("Rows are not equal length") exit() #Cycle through each item, since random grid must brute force solution #Alg is 0(n^2) greatestProd = 0 for y in range(0,height): for x in range(0,length): if (x <= length-4): greatestProd = max(grid[y][x] * grid[y][x+1] * grid[y][x+2] * grid[y][x+3],greatestProd) if (y <= height - 4): greatestProd = max(grid[y][x] * grid[y+1][x] * grid[y+2][x] * grid[y+3][x],greatestProd) if (y <= height - 4 and x < length-4): greatestProd = max(grid[y][x] * grid[y+1][x+1] * grid[y+2][x+2] * grid[y+3][x+3],greatestProd) if (y <= height - 4 and x >= 3): greatestProd = max(grid[y][x] * grid[y+1][x-1] * grid[y+2][x-2] * grid[y+3][x-3], greatestProd) print("Result: " + str(greatestProd)) if __name__ == "__main__": print("Problem 11") print("What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?") filePath = "./InputFiles/gridP11.txt" grid = EulerSupport.parseUnicodeSepFile(filePath, True) findLargest(grid)
@EulerSupport.printTiming #This function works by cylcing through each least significant digit and adding that number #Then taking the mod 10 of that number and making it the result and then carry the sum over to #the next column of digits def addSplit(nums): word = "" length = len(nums[0][0]) print("Length: " + str(length)) carry = 0 for pos in range(length - 1, -1, -1): #49..0 for num in nums: carry += int(num[0][pos]) dangle = carry % 10 carry -= dangle carry = int(carry / 10) word = str(dangle) + word word = str(carry) + word print(word[:10]) return (word[:10]) if __name__ == "__main__": print("Problem 13") filePath = "./InputFiles/numbersP13.txt" nums = EulerSupport.parseUnicodeSepFile(filePath, False) #This way solved it more cleverly and would be useful in a larger system EulerSupport.printTiming(addSplit(nums)) #This is the way that just summed all the numbers using python EulerSupport.printTiming(cheatway(nums))
#Alg is 0(n^2) greatestProd = 0 for y in range(0, height): for x in range(0, length): if (x <= length - 4): greatestProd = max( grid[y][x] * grid[y][x + 1] * grid[y][x + 2] * grid[y][x + 3], greatestProd) if (y <= height - 4): greatestProd = max( grid[y][x] * grid[y + 1][x] * grid[y + 2][x] * grid[y + 3][x], greatestProd) if (y <= height - 4 and x < length - 4): greatestProd = max( grid[y][x] * grid[y + 1][x + 1] * grid[y + 2][x + 2] * grid[y + 3][x + 3], greatestProd) if (y <= height - 4 and x >= 3): greatestProd = max( grid[y][x] * grid[y + 1][x - 1] * grid[y + 2][x - 2] * grid[y + 3][x - 3], greatestProd) print("Result: " + str(greatestProd)) if __name__ == "__main__": print("Problem 11") print( "What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?" ) filePath = "./InputFiles/gridP11.txt" grid = EulerSupport.parseUnicodeSepFile(filePath, True) findLargest(grid)
@EulerSupport.printTiming #This function works by cylcing through each least significant digit and adding that number #Then taking the mod 10 of that number and making it the result and then carry the sum over to #the next column of digits def addSplit(nums): word = "" length = len(nums[0][0]) print("Length: " + str(length)) carry = 0 for pos in range(length-1,-1,-1): #49..0 for num in nums: carry += int(num[0][pos]) dangle = carry % 10 carry -= dangle carry = int(carry / 10) word = str(dangle) + word word = str(carry) + word print(word[:10]) return(word[:10]) if __name__ == "__main__": print("Problem 13") filePath = "./InputFiles/numbersP13.txt" nums = EulerSupport.parseUnicodeSepFile(filePath, False) #This way solved it more cleverly and would be useful in a larger system EulerSupport.printTiming(addSplit(nums)) #This is the way that just summed all the numbers using python EulerSupport.printTiming(cheatway(nums))