You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
6 years ago
|
from time import sleep
|
||
|
|
||
|
m = []
|
||
|
|
||
|
with open('input.txt', 'r') as f:
|
||
|
for line in f.readlines():
|
||
|
row = []
|
||
|
for sym in line.strip():
|
||
|
row.append(sym)
|
||
|
m.append(row)
|
||
|
|
||
|
def print_map():
|
||
|
for row in m:
|
||
|
print(''.join(row))
|
||
|
|
||
|
print_map()
|
||
|
|
||
|
def get_adjacent(x, y):
|
||
|
adj = []
|
||
|
for i in [-1, 0, 1]:
|
||
|
for j in [-1, 0, 1]:
|
||
|
if not (i == 0 and j == 0):
|
||
|
if x + i < 0 or y + j < 0:
|
||
|
continue
|
||
|
try:
|
||
|
adj.append(m[x + i][y + j])
|
||
|
except:
|
||
|
pass
|
||
|
return adj
|
||
|
|
||
|
for i in range(10):
|
||
|
new_m = []
|
||
|
for x, row in enumerate(m):
|
||
|
new_row = []
|
||
|
for y, sym in enumerate(row):
|
||
|
adj = get_adjacent(x, y)
|
||
|
num_trees = adj.count('|')
|
||
|
num_lumb = adj.count('#')
|
||
|
if sym == '.':
|
||
|
res = '|' if num_trees >= 3 else sym
|
||
|
elif sym == '|':
|
||
|
res = '#' if num_lumb >= 3 else sym
|
||
|
elif sym == '#':
|
||
|
res = sym if num_lumb >= 1 and num_trees >= 1 else '.'
|
||
|
# print(f'[{x}, {y}]: {sym} became {res}. Trees: {num_trees}, Lumber: {num_lumb}, Neighbors: {len(adj)}')
|
||
|
new_row.append(res)
|
||
|
new_m.append(new_row)
|
||
|
m = new_m
|
||
|
print()
|
||
|
print(f'Minute {i + 1}:')
|
||
|
print_map()
|
||
|
sleep(1)
|
||
|
|
||
|
|
||
|
num_trees = sum([row.count('|') for row in m])
|
||
|
num_lumb = sum([row.count('#') for row in m])
|
||
|
num_open = sum([row.count('.') for row in m])
|
||
|
|
||
|
print(f'Trees: {num_trees}, Lumber: {num_lumb}, Open: {num_open}')
|
||
|
print(f'Result: {num_lumb * num_trees}')
|