This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_chronal_charge.py
48 lines (37 loc) · 1.6 KB
/
11_chronal_charge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
##################################
# --- Day 11: Chronal Charge --- #
##################################
import AOCUtils
def getMaxSum(m, sqSize):
maxSum, maxCoords = None, None
for x in range(sqSize-1, 300):
for y in range(sqSize-1, 300):
# 2D Partial Sum
curSum = partials[x][y]
if x-sqSize >= 0: curSum -= partials[x-sqSize][y]
if y-sqSize >= 0: curSum -= partials[x][y-sqSize]
if x-sqSize >= 0 and y-sqSize >= 0: curSum += partials[x-sqSize][y-sqSize]
if not maxSum or curSum > maxSum:
maxSum, maxCoords = curSum, (x-(sqSize-1), y-(sqSize-1))
return maxSum, maxCoords
##################################
serial = AOCUtils.loadInput(11)
cells = [[None for _ in range(300)] for _ in range(300)]
for x in range(300):
for y in range(300):
cells[x][y] = ((((x+10)*y + serial) * (x+10) // 100) % 10) - 5
partials = [[cells[x][y] for y in range(300)] for x in range(300)]
for x in range(300):
for y in range(300):
if x-1 >= 0: partials[x][y] += partials[x-1][y]
if y-1 >= 0: partials[x][y] += partials[x][y-1]
if x-1 >= 0 and y-1 >= 0: partials[x][y] -= partials[x-1][y-1]
curSum, curCoords = getMaxSum(partials, 3)
print("Part 1: {},{}".format(curCoords[0], curCoords[1]))
maxSum, maxCoords, maxSize = None, None, None
for i in range(1, 300+1):
curSum, curCoords = getMaxSum(partials, i)
if not maxSum or curSum > maxSum:
maxSum, maxCoords, maxSize = curSum, curCoords, i
print("Part 2: {},{},{}".format(maxCoords[0], maxCoords[1], maxSize))
AOCUtils.printTimeTaken()