-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp20.py
40 lines (33 loc) · 972 Bytes
/
p20.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# https://adventofcode.com/2021/day/20
# Created by: Menaka S. 20 Dec 2021
import sys
import copy
mx = 100
off = 100
step = 0
mxstep = 50
ctr = 0
matrix = [['.' for k in range(-off,mx+off)] for j in range(-off,mx+off)]
def getneighbours(x,y):
for i in range(x-1,x+2):
for j in range(y-1,y+2):
yield (i,j)
def getval(x,y):
binstr = ''
for (i,j) in getneighbours(x,y):
if 0 <= i < len(matrix) and 0 <= j < len(matrix):
binstr = binstr + str(int(matrix[i][j] == '#'))
return int(binstr,2)
iea,_,*lines = sys.stdin.read().splitlines()
for line in lines:
for i in range(len(line)):
matrix[ctr+off][i+off] = line[i]
ctr +=1
newmatrix = []
while step < mxstep:
newmatrix = [[iea[getval(i,j)] for j in range(len(matrix[0]))] for i in range(len(matrix))]
matrix = copy.deepcopy(newmatrix)
ct = sum(int(matrix[i][j]=='#') for j in range(mxstep,len(matrix[0])-mxstep) for i in range(mxstep,len(matrix)-mxstep))
step+=1
if step in (2,50):
print(ct)