Posted By

chroto on 05/21/10


Tagged


Versions (?)

Project Euler - Problem 11


 / Published in: Python
 

  1. #!/usr/bin/python
  2.  
  3. # Project Euler - Problem 11
  4.  
  5. print "\nProject Euler - Problem 11"
  6. print "In a 20x20 grid, find the greatest"
  7. print "product of 4 adjacent numbers."
  8.  
  9. f = open("problem11.grid",'r')
  10. x = []
  11. ans = 0
  12. beef = True
  13. # Load data into 20x20 list
  14. for m in xrange(20):
  15. x.append([])
  16. for n in xrange(20):
  17. x[m].append(int(f.read(3)))
  18.  
  19. for m in xrange(16):
  20. for n in xrange(16):
  21. prod=[]
  22. # Multiply Rows
  23. prod.append(x[m][n] * x[m][n+1] * x[m][n+2] * x[m][n+3])
  24. prod.append(x[m+1][n] * x[m+1][n+1] * x[m+1][n+2] * x[m+1][n+3])
  25. prod.append(x[m+2][n] * x[m+2][n+1] * x[m+2][n+2] * x[m+2][n+3])
  26. prod.append(x[m+3][n] * x[m+3][n+1] * x[m+3][n+2] * x[m+3][n+3])
  27. # Multiply Columns
  28. prod.append(x[m][n] * x[m+1][n] * x[m+2][n] * x[m+3][n])
  29. prod.append(x[m][n+1] * x[m+1][n+1] * x[m+2][n+1] * x[m+3][n+1])
  30. prod.append(x[m][n+2] * x[m+1][n+2] * x[m+2][n+2] * x[m+3][n+2])
  31. prod.append(x[m][n+3] * x[m+1][n+3] * x[m+2][n+3] * x[m+3][n+3])
  32. # Diagonals
  33. prod.append(x[m][n] * x[m+1][n+1] * x[m+2][n+2] * x[m+3][n+3])
  34. prod.append(x[m+3][n] * x[m+2][n+1] * x[m+1][n+2] * x[m][n+3])
  35. if max(prod) > ans:
  36. ans = max(prod)
  37.  
  38. print ans

Report this snippet  

You need to login to post a comment.