my advent of code 2021 solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.6 KiB

3 years ago
import sys
basins = []
class basin:
def __init__(self, y, x):
self.startpoi = [y , x]
def generate(self, gri):
self.poi = []
self.poi.append(self.startpoi)
for i in self.poi:
if i[1] == 0:
pass
elif int(gri.y[i[0]][i[1]]) < int(gri.y[i[0]][i[1] - 1]) and int(gri.y[i[0]][i[1] - 1]) != 9:
if not [i[0], i[1] - 1] in self.poi:
self.poi.append([i[0], i[1] - 1])
if i[1] == len(gri.y[i[0]]) - 1:
pass
elif int(gri.y[i[0]][i[1]]) < int(gri.y[i[0]][i[1] + 1]) and int(gri.y[i[0]][i[1] + 1]) != 9:
if not [i[0], i[1] + 1] in self.poi:
self.poi.append([i[0], i[1] + 1])
if i[0] == 0:
pass
elif int(gri.y[i[0]][i[1]]) < int(gri.y[i[0] - 1][i[1]]) and int(gri.y[i[0] - 1][i[1]]) != 9:
if not [i[0] - 1, i[1]] in self.poi:
self.poi.append([i[0] - 1, i[1]])
if i[0] == len(gri.y) - 1:
pass
elif int(gri.y[i[0]][i[1]]) < int(gri.y[i[0] + 1][i[1]]) and int(gri.y[i[0] + 1][i[1]]) != 9:
if not [i[0] + 1, i[1]] in self.poi:
self.poi.append([i[0] + 1, i[1]])
return len(self.poi)
class grid:
def __init__(self):
self.y = []
def addinp(self, inp):
self.y.append(inp)
def locate(self):
solucounter = 0
for i in range(len(self.y)):
for t in range(len(self.y[i])):
counter = 0
if t == 0:
counter += 1
elif int(self.y[i][t]) < int(self.y[i][t - 1]):
counter += 1
if t == len(self.y[i]) - 1:
counter += 1
elif int(self.y[i][t]) < int(self.y[i][t + 1]):
counter += 1
if i == 0:
counter += 1
elif int(self.y[i][t]) < int(self.y[i - 1][t]):
counter += 1
if i == len(self.y) - 1:
counter += 1
elif int(self.y[i][t]) < int(self.y[i + 1][t]):
counter += 1
if counter == 4:
basins.append(basin(i, t))
solucounter += 1 + int(self.y[i][t])
return solucounter
grit = grid()
solv = []
with open(sys.argv[1], 'r') as f:
for i in f.readlines():
i = i.strip()
grit.addinp(i)
print(grit.locate())
for i in basins:
solv.append(i.generate(grit))
solv = sorted(solv, reverse = True)
print(solv[0:3])
print(solv[0] * solv[1] * solv[2])