import sys class octopie: def __init__(self): self.temp = [] def format(self): self.y = [] temp2 = [] for i in self.temp: temp2 = [] temp = list(i) for t in temp: t = int(t) temp2.append(t) self.y.append(temp2) def tick(self): for i in range(len(self.y)): for t in range(len(self.y[i])): self.y[i][t] += 1 def flash(self): pas = True counter = 0 while pas: pas = False for i in range(len(self.y)): for t in range(len(self.y[i])): if self.y[i][t] > 9: self.y[i][t] = 0 pas = True counter += 1 if t == 0: pass elif self.y[i][t - 1] != 0: self.y[i][t - 1] += 1 if t == len(self.y[i]) - 1: pass elif self.y[i][t + 1] != 0: self.y[i][t + 1] += 1 if i == 0: pass elif self.y[i - 1][t] != 0: self.y[i - 1][t] += 1 if i == len(self.y) - 1: pass elif self.y[i + 1][t] != 0: self.y[i + 1][t] += 1 if t == 0 or i == 0: pass elif self.y[i - 1][t - 1] != 0: self.y[i - 1][t - 1] += 1 if t == len(self.y[i]) - 1 or i == 0: pass elif self.y[i - 1][t + 1] != 0: self.y[i - 1][t + 1] += 1 if t == 0 or i == len(self.y) - 1: pass elif self.y[i + 1][t - 1] != 0: self.y[i + 1][t - 1] += 1 if t == len(self.y[i]) - 1 or i == len(self.y) - 1: pass elif self.y[i + 1][t + 1] != 0: self.y[i + 1][t + 1] += 1 return counter octo = octopie() solution = 0 with open(sys.argv[1], 'r') as f: for i in f.readlines(): i = i.strip() octo.temp.append(i) octo.format() for i in range(100): octo.tick() solution += octo.flash() print(solution)