Guest User

Untitled

a guest
Dec 11th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. let totalSize = 300
  2.  
  3. func powerLevel(x: Int, y: Int, serialNumber: Int) -> Int {
  4. let rackID = x + 10
  5. return ((rackID * y + serialNumber) * rackID / 100) % 10 - 5
  6. }
  7.  
  8. func part2(serialNumber: Int) -> String {
  9. let grid = (1...totalSize).map { y in
  10. (1...totalSize).map { x in
  11. powerLevel(x: x, y: y, serialNumber: serialNumber)
  12. }
  13. }
  14.  
  15. let partialX = grid.map { $0.scan(0, +) }
  16. let partial = (0..<totalSize).map { x in partialX.lazy.map { $0[x] }.scan(0, +) }
  17.  
  18. var maxSum = Int.min
  19. var solution: (x: Int, y: Int, size: Int)!
  20.  
  21. for size in 1...totalSize {
  22. for x in 0..<(totalSize - size) {
  23. for y in 0..<(totalSize - size) {
  24. let sum = partial[x + size][y + size]
  25. - partial[x][y + size]
  26. - partial[x + size][y]
  27. + partial[x][y]
  28.  
  29. if sum > maxSum {
  30. maxSum = sum
  31. solution = (x + 1, y + 1, size)
  32. }
  33. }
  34. }
  35. }
  36.  
  37. return "\(solution.x),\(solution.y),\(solution.size)"
  38. }
Add Comment
Please, Sign In to add comment