Created
December 12, 2020 07:37
-
-
Save rendyanthony/f12f26282266bb9410dd9b2f52ab4789 to your computer and use it in GitHub Desktop.
Advent of Code Day 11 (Part 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
def num_adj_occupied_2(x, y, in_array): | |
num = 0 | |
dim_x, dim_y = len(in_array[0]), len(in_array) | |
for ym in (-1, 0, 1): | |
for xm in (-1, 0, 1): | |
if ym == 0 and xm == 0: continue | |
xp, yp = x+xm, y+ym | |
while (xp >= 0 and xp < dim_x) and (yp >= 0 and yp < dim_y): | |
view = in_array[yp][xp] | |
if view != '.': | |
if view == '#': | |
num += 1 | |
break | |
xp += xm | |
yp += ym | |
return num | |
def do_round(in_array): | |
out_array = copy.deepcopy(in_array) | |
for y, row in enumerate(in_array): | |
for x, seat in enumerate(row): | |
if seat == 'L': | |
if num_adj_occupied_2(x, y, in_array) == 0: | |
out_array[y][x] = '#' | |
if seat == '#': | |
if num_adj_occupied_2(x, y, in_array) >= 5: | |
out_array[y][x] = 'L' | |
return out_array | |
array = [list(row.strip()) for row in open("input_11").readlines()] | |
prev_occ, num_occ = 0, 0 | |
while True: | |
array = do_round(array) | |
prev_occ, num_occ = num_occ, sum([row.count('#') for row in array]) | |
if num_occ == prev_occ: | |
print(num_occ) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment