Last active
December 14, 2020 16:20
-
-
Save pjhoberman/dca938bf4404a75afec2aec02cfe5355 to your computer and use it in GitHub Desktop.
This file contains 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
from collections import Counter | |
# part 1 | |
with open('scratch_28.txt') as file: | |
data = file.read().splitlines() | |
def get_adjacent_seats(row, col, _data): | |
previous_row = None | |
next_row = None | |
previous_col = None | |
next_col = None | |
# is there a previous row? | |
if row > 0: | |
previous_row = row - 1 | |
# is there a next row? | |
if row < len(_data) - 1: | |
next_row = row + 1 | |
# is there a previous col? | |
if col > 0: | |
previous_col = col - 1 | |
# is there a next col? | |
if col < len(_data[0]) - 1: | |
next_col = col + 1 | |
adjacents = [] | |
if previous_row is not None: | |
if previous_col is not None: | |
adjacents.append(_data[previous_row][previous_col]) | |
adjacents.append(_data[previous_row][col]) | |
if next_col is not None: | |
adjacents.append(_data[previous_row][next_col]) | |
if previous_col is not None: | |
adjacents.append(_data[row][previous_col]) | |
if next_col is not None: | |
adjacents.append(_data[row][next_col]) | |
if next_row is not None: | |
if previous_col is not None: | |
adjacents.append(_data[next_row][previous_col]) | |
adjacents.append(_data[next_row][col]) | |
if next_col is not None: | |
adjacents.append(_data[next_row][next_col]) | |
return adjacents | |
def make_moves(row, col, previous_pass, new_data): | |
if len(new_data) <= row: | |
new_data.append([]) | |
if previous_pass[row][col] == ".": | |
new_data[row].append(".") | |
elif previous_pass[row][col] == "L" and "#" not in get_adjacent_seats(row, col, previous_pass): | |
new_data[row].append("#") | |
elif previous_pass[row][col] == "#" and Counter(get_adjacent_seats(row, col, previous_pass))["#"] >= 4: | |
new_data[row].append("L") | |
else: | |
new_data[row].append(previous_pass[row][col]) | |
return new_data | |
def run_it(): | |
previous_pass = data | |
while True: | |
new_data=[[]] | |
for row in range(len(data)): | |
for col in range(len(data[0])): | |
new_data = make_moves(row, col, previous_pass, new_data) | |
if new_data == previous_pass: | |
break | |
previous_pass = new_data | |
return Counter("".join(["".join(row) for row in new_data])) | |
# part 2 | |
with open('scratch_28.txt') as file: | |
data = file.read().splitlines() | |
def get_all_adjacents(row, col, _data): | |
adjacents = [] | |
# vertical | |
i = 1 | |
step1 = True | |
step2 = True | |
while (step1 or step2) and i < 20: | |
if step1: | |
try: | |
adjacents.append(_data[row+i][col]) | |
if _data[row+i][col] in ["L", "#"]: | |
step1 = False | |
except IndexError: | |
step1 = False | |
if row-i >= 0 and step2: | |
try: | |
adjacents.append(_data[row-i][col]) | |
if _data[row-i][col] in ["L", "#"]: | |
step2 = False | |
except IndexError: | |
step2 = False | |
else: | |
step2 = False | |
i += 1 | |
# horizontal | |
i = 1 | |
step1 = True | |
step2 = True | |
while (step1 or step2) and i < 20: | |
if col - i >= 0 and step1: | |
try: | |
adjacents.append(_data[row][col-i]) | |
if _data[row][col-i] in ["L", "#"]: | |
step1 = False | |
except IndexError: | |
step1 = False | |
else: | |
step1 = False | |
if step2: | |
try: | |
adjacents.append(_data[row][col+i]) | |
if _data[row][col+i] in ["L", "#"]: | |
step2 = False | |
except IndexError: | |
step2 = False | |
i += 1 | |
# diagonal 1 | |
i = 1 | |
step1 = True | |
step2 = True | |
while (step1 or step2) and i < 20: | |
if step1: | |
try: | |
adjacents.append(_data[row + i][col + i]) | |
if _data[row + i][col + i] in ["L", "#"]: | |
step1 = False | |
except IndexError: | |
step1 = False | |
if step2 and row-i >= 0 and col-i >= 0: | |
try: | |
adjacents.append(_data[row - i][col - i]) | |
if _data[row - i][col - i] in ["L", "#"]: | |
step2 = False | |
except IndexError: | |
step2 = False | |
else: | |
step2 = False | |
i += 1 | |
# diagonal 2 | |
i = 1 | |
step1 = True | |
step2 = True | |
while (step1 or step2) and i < 20: | |
if step1 and col -i >= 0: | |
try: | |
adjacents.append(_data[row + i][col - i]) | |
if _data[row + i][col - i] in ["L", "#"]: | |
step1 = False | |
except IndexError: | |
step1 = False | |
else: | |
step1 = False | |
if step2 and row -i >= 0: | |
try: | |
adjacents.append(_data[row - i][col + i]) | |
if _data[row - i][col + i] in ["L", "#"]: | |
step2 = False | |
except IndexError: | |
step2 = False | |
else: | |
step2 = False | |
i += 1 | |
return adjacents | |
def make_moves2(row, col, previous_pass, new_data): | |
if len(new_data) <= row: | |
new_data.append([]) | |
if previous_pass[row][col] == ".": | |
new_data[row].append(".") | |
elif previous_pass[row][col] == "L" and "#" not in get_all_adjacents(row, col, previous_pass): | |
new_data[row].append("#") | |
elif previous_pass[row][col] == "#" and Counter(get_all_adjacents(row, col, previous_pass))["#"] >= 5: | |
new_data[row].append("L") | |
else: | |
new_data[row].append(previous_pass[row][col]) | |
return new_data | |
def run_it2(): | |
previous_pass = data | |
while True: | |
new_data=[[]] | |
for row in range(len(data)): | |
for col in range(len(data[0])): | |
new_data = make_moves2(row, col, previous_pass, new_data) | |
if new_data == previous_pass: | |
break | |
previous_pass = new_data | |
return Counter("".join(["".join(row) for row in new_data])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment