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.
74 lines
2.0 KiB
74 lines
2.0 KiB
import sys |
|
|
|
class img: |
|
def __init__(self): |
|
self.y = [] |
|
|
|
def addline(self, line): |
|
self.y.append(line) |
|
|
|
def get_num(self,y,x): |
|
number='' |
|
if y != 0 and x != 0: number += self.y[y - 1][x - 1] |
|
else: number += '.' |
|
if y !=0: number += self.y[y - 1][x] |
|
else: number += '.' |
|
if y != 0 and x != len(self.y[0]) - 1: number += self.y[y - 1][x + 1] |
|
else: number += '.' |
|
if x != 0: number += self.y[y][x - 1] |
|
else: number += '.' |
|
number += self.y[y][x] |
|
if x != len(self.y[0]) - 1: number += self.y[y][x + 1] |
|
else: number += '.' |
|
if y != len(self.y) - 1 and x != 0: number += self.y[y + 1][x - 1] |
|
else: number += '.' |
|
if y != len(self.y) - 1: number += self.y[y + 1][x] |
|
else: number += '.' |
|
if y != len(self.y) - 1 and x != len(self.y[0]) - 1: number += self.y[y + 1][x + 1] |
|
else: number += '.' |
|
number = number.replace('.', '0') |
|
number = number.replace('#', '1') |
|
|
|
return (int(number, 2)) |
|
|
|
def enhance(self): |
|
new_image = [] |
|
self.y.insert(0, '.'*len(self.y[0])) |
|
self.y.append('.'*len(self.y[0])) |
|
for i in range(len(self.y)): |
|
self.y[i] = '.' + self.y[i] + '.' |
|
for i in range(len(self.y)): |
|
new_line = '' |
|
for t in range(len(self.y[i])): |
|
num = self.get_num(i,t) |
|
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) |
|
|
|
image.enhance() |
|
image.enhance() |
|
|
|
print(image.count()) |
|
|
|
|