Solve 2015 days 11-18.
This commit is contained in:
40
2015/d18.py
Normal file
40
2015/d18.py
Normal file
@@ -0,0 +1,40 @@
|
||||
data = open(0).read().strip()
|
||||
|
||||
part_2 = False
|
||||
rows = list(map(str, data.splitlines()))
|
||||
nrows = len(rows)
|
||||
ncols = len(rows[0])
|
||||
|
||||
for _ in range(100):
|
||||
new_field = []
|
||||
for r in range(nrows):
|
||||
nrow = []
|
||||
for c in range(ncols):
|
||||
count = 0
|
||||
for nb in [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]:
|
||||
nr = r + nb[0]
|
||||
nc = c + nb[1]
|
||||
if nr < 0 or nr >= nrows or nc < 0 or nc >= ncols:
|
||||
continue
|
||||
if rows[nr][nc] == "#":
|
||||
count += 1
|
||||
if rows[r][c] == "#" and (count == 2 or count == 3):
|
||||
nrow.append("#")
|
||||
elif rows[r][c] == "." and count == 3:
|
||||
nrow.append("#")
|
||||
else:
|
||||
nrow.append(".")
|
||||
new_field.append(nrow)
|
||||
rows = new_field
|
||||
if part_2:
|
||||
rows[0][0] = "#"
|
||||
rows[0][ncols - 1] = "#"
|
||||
rows[nrows - 1][0] = "#"
|
||||
rows[nrows - 1][ncols - 1] = "#"
|
||||
|
||||
r = 0
|
||||
for row in rows:
|
||||
for c in row:
|
||||
if c == "#":
|
||||
r += 1
|
||||
print(r)
|
||||
Reference in New Issue
Block a user