/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/python # Project Euler - Problem 11 print "\nProject Euler - Problem 11" print "In a 20x20 grid, find the greatest" print "product of 4 adjacent numbers." f = open("problem11.grid",'r') x = [] ans = 0 beef = True # Load data into 20x20 list for m in xrange(20): x.append([]) for n in xrange(20): x[m].append(int(f.read(3))) for m in xrange(16): for n in xrange(16): prod=[] # Multiply Rows prod.append(x[m][n] * x[m][n+1] * x[m][n+2] * x[m][n+3]) prod.append(x[m+1][n] * x[m+1][n+1] * x[m+1][n+2] * x[m+1][n+3]) prod.append(x[m+2][n] * x[m+2][n+1] * x[m+2][n+2] * x[m+2][n+3]) prod.append(x[m+3][n] * x[m+3][n+1] * x[m+3][n+2] * x[m+3][n+3]) # Multiply Columns prod.append(x[m][n] * x[m+1][n] * x[m+2][n] * x[m+3][n]) prod.append(x[m][n+1] * x[m+1][n+1] * x[m+2][n+1] * x[m+3][n+1]) prod.append(x[m][n+2] * x[m+1][n+2] * x[m+2][n+2] * x[m+3][n+2]) prod.append(x[m][n+3] * x[m+1][n+3] * x[m+2][n+3] * x[m+3][n+3]) # Diagonals prod.append(x[m][n] * x[m+1][n+1] * x[m+2][n+2] * x[m+3][n+3]) prod.append(x[m+3][n] * x[m+2][n+1] * x[m+1][n+2] * x[m][n+3]) if max(prod) > ans: ans = max(prod) print ans