Advertisement
Evoid-null

Advent of code 2021 day 9 part 2

Dec 9th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. def countbasin(hm,i,j,cnt = 0):
  2.     if hm[i][j] < 9:
  3.         cnt+=1
  4.         hm[i][j]=9
  5.         for x,y in [(0,-1), (0,1), (-1,0), (1,0)]:
  6.             hm,cnt = countbasin(hm,i+x,j+y,cnt)
  7.     return hm,cnt
  8.  
  9. hm = [list(map(int,list('9'+i.replace("\n","")+'9'))) for i in open("input.txt").readlines()]
  10. hm = [[9]*len(hm[0])] + hm + [[9]*len(hm[0])]
  11.  
  12. basins = [countbasin(hm,i,j)[1] for i in range(1,len(hm)-1) for j in range(1,len(hm[0])-1) if ((hm[i][j] < hm[i][j-1]) and (hm[i][j] < hm[i][j+1]) and (hm[i][j] < hm[i-1][j]) and (hm[i][j] < hm[i+1][j]))]
  13.  
  14. basins.sort(reverse=True)
  15. print(basins[0]*basins[1]*basins[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement