Skip to content

Commit 709d4a4

Browse files
author
Joe Webster
committed
Adding day 9 part 2. Should relly refactor these down into one like I planned but ZFG
1 parent a34f422 commit 709d4a4

1 file changed

Lines changed: 50 additions & 13 deletions

File tree

9/__init__.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ def detect_basins(self, algorithm):
4343

4444
for row in range(0, self.height+1):
4545
for col in range(0, self.width+1):
46-
algo_fn(row, col)
46+
basin = algo_fn(row, col, [])
47+
if basin:
48+
self.basins.append(basin)
4749

48-
print(self.status())
50+
self.status()
4951

5052
def status(self):
5153
ret = []
@@ -64,7 +66,7 @@ def status(self):
6466
line += str(val)
6567
print(line)
6668

67-
def crawl_part_1(self, row, col):
69+
def crawl_part_1(self, row, col, foo):
6870
basin = []
6971

7072
self.mark_visited(row, col)
@@ -92,10 +94,37 @@ def crawl_part_1(self, row, col):
9294

9395
self.basins.append([[row, col, origin_val]])
9496

95-
def crawl_part_2(self, row, col):
96-
pass
97+
def crawl_part_2(self, row, col, basin):
98+
if self.is_visited(row, col):
99+
return
97100

98-
def calculate_risk_level(self, basins):
101+
self.mark_visited(row, col)
102+
103+
origin_val = self.value_at(row, col)
104+
105+
if origin_val == 9:
106+
return
107+
else:
108+
basin.append([row, col, origin_val])
109+
110+
scan_points = [
111+
[row-1, col],
112+
[row+1, col],
113+
[row, col-1],
114+
[row, col+1]
115+
]
116+
117+
for target in scan_points:
118+
target_val = self.value_at(target[0], target[1])
119+
120+
if target_val == None: #Out of bounds
121+
continue
122+
123+
self.crawl_part_2(target[0], target[1], basin)
124+
125+
return basin
126+
127+
def calculate_risk_level(self):
99128
risk = 0
100129

101130
for basin in self.basins:
@@ -104,6 +133,11 @@ def calculate_risk_level(self, basins):
104133

105134
return risk
106135

136+
def calculate_size_product(self):
137+
sizes = [len(b) for b in self.basins]
138+
sizes.sort()
139+
140+
return sizes[-1] * sizes[-2] * sizes[-3]
107141

108142
def bcolor(color, text):
109143
colors = {
@@ -125,23 +159,26 @@ def solution_part_1(data):
125159
an = AutoNavigation()
126160

127161
an.load_data(data)
128-
lp = an.detect_basins('crawl_part_1')
129-
130-
rl = an.calculate_risk_level(lp)
162+
an.detect_basins('crawl_part_1')
163+
rl = an.calculate_risk_level()
131164
print('Risk Level:',rl)
132165

133166
def solution_part_2(data):
134167
an = AutoNavigation()
135168

136169
an.load_data(data)
137-
lp = an.detect_basins('crawl_part_2')
138-
139-
rl = an.calculate_risk_level(lp)
140-
print('Risk Level:',rl)
170+
an.detect_basins('crawl_part_2')
171+
print("Size Product:", an.calculate_size_product())
141172

142173
if __name__ == '__main__':
143174
data = get_test_data()
144175
solution_part_1(data)
145176

146177
data = get_data()
147178
solution_part_1(data)
179+
180+
data = get_test_data()
181+
solution_part_2(data)
182+
183+
data = get_data()
184+
solution_part_2(data)

0 commit comments

Comments
 (0)