-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
73 lines (56 loc) · 1.82 KB
/
day9.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
with open('day9input.txt') as f:
flows = f.read().splitlines()
flowsToMatrix = []
for flow in flows:
curLine = [*flow] # * converts 123 into 1 2 3 and [] coverts it into [1,2,3]
flowsToMatrix.append(list(map(int, curLine)))
rows = len(flowsToMatrix)
cols = len(flowsToMatrix[0])
def lowestPoint(row, col, point):
# Check Right
if col + 1 < cols:
if flowsToMatrix[row][col+1] <= point:
return False
if row + 1 < rows:
if flowsToMatrix[row+1][col] <= point:
return False
if row - 1 >= 0:
if flowsToMatrix[row-1][col] <= point:
return False
if col - 1 >= 0:
if flowsToMatrix[row][col-1] <= point:
return False
return True
lowestPoints = []
lpCords = []
for row in range(rows):
for col in range(cols):
# Right, Left, Top and Bottom
if lowestPoint(row, col, flowsToMatrix[row][col]):
lowestPoints.append(flowsToMatrix[row][col])
lpCords.append((row, col))
def increaseByOne(element):
return element + 1
print("Part 1:", sum(list(map(increaseByOne, lowestPoints)))) # 607
# Simple DFS, start from the smallest points calculated above (lpCords)
# Part 2:
def dfs(x, y, prevValue):
if ( x < 0 or x == rows or y < 0 or y == cols or flowsToMatrix[x][y] <= prevValue or flowsToMatrix[x][y] == 9 or flowsToMatrix[x][y] == -1):
return 0
value = flowsToMatrix[x][y]
flowsToMatrix[x][y] = -1
total = 1
total += dfs(x+1, y, value)
total += dfs(x-1, y, value)
total += dfs(x, y+1, value)
total += dfs(x, y-1, value)
return total
points = []
for lowestPoint in lpCords:
x, y = lowestPoint
total = dfs(x, y, -2)
points.append(total)
product = 1
for point in sorted(points)[::-1][:3]:
product *= point
print("Part 2:", product) # 900864