import sys class img: def __init__(self): self.y = [] def addline(self, line): self.y.append(line) def get_num(self,y,x, test): number='' if y != 0 and x != 0: number += self.y[y - 1][x - 1] else: number += test if y !=0: number += self.y[y - 1][x] else: number += test if y != 0 and x != len(self.y[0]) - 1: number += self.y[y - 1][x + 1] else: number += test if x != 0: number += self.y[y][x - 1] else: number += test number += self.y[y][x] if x != len(self.y[0]) - 1: number += self.y[y][x + 1] else: number += test if y != len(self.y) - 1 and x != 0: number += self.y[y + 1][x - 1] else: number += test if y != len(self.y) - 1: number += self.y[y + 1][x] else: number += test if y != len(self.y) - 1 and x != len(self.y[0]) - 1: number += self.y[y + 1][x + 1] else: number += test number = number.replace('.', '0') number = number.replace('#', '1') return (int(number, 2)) def enhance(self, test): new_image = [] self.y.insert(0, test*len(self.y[0])) self.y.append(test*len(self.y[0])) for i in range(len(self.y)): self.y[i] = test + self.y[i] + test for i in range(len(self.y)): new_line = '' for t in range(len(self.y[i])): num = self.get_num(i,t,test) new_line += algorithm[num] new_image.append(new_line) self.y = new_image.copy() def count(self): counter = 0 for f in self.y: for g in f: if g == '#': counter += 1 return(counter) image = img() with open(sys.argv[1], 'r') as f: toggle = False for g in f.readlines(): g = g.strip() if g == '': toggle = True if not toggle: algorithm = g if toggle and g != '': image.addline(g) for i in range(25): image.enhance('.') image.enhance('#') print(image.count())