-
-
Save chriswmartin/3e0b42f64bbbbd215d72e0ee8d5100e2 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
import os | |
import time | |
import random | |
from rich import print | |
from rich.live import Live | |
def generate_layout(rows, columns): | |
chars = ['L', '.'] | |
layout = [] | |
for c in range(columns): | |
row = [] | |
for r in range(rows): | |
row.append(random.choice(chars)) | |
layout.append(row) | |
return(layout) | |
def print_layout(seat_array): | |
layout = "" | |
for column in seat_array: | |
txt = "" | |
for seat in column: | |
if seat == '.': txt += "[yellow]" + " " + "[/yellow]" | |
if seat == '#': txt += "[red]" + "▓" + "[/red]" | |
if seat == 'L': txt += "[green]" + '▒' + "[/green]" | |
if seat == 'X': txt += "[black]" + seat + "[/black]" | |
layout += '\n' + txt | |
return(layout) | |
def model_seats(layout): | |
rows = len(layout) | |
columns = len(layout[0]) | |
seats = [] | |
for r in range(rows): | |
seat_cols = [] | |
for c in range(columns): | |
dist = columns | |
current = layout[r][c] | |
left = layout[r][c-1] if c-1 in range(columns) else 'X' | |
if left == '.': | |
for i in range(1, dist): | |
left = layout[r][c-i] if c-i in range(columns) else 'X' | |
if left == 'L' or left == '#' or left == 'X': break | |
right = layout[r][c+1] if c+1 in range(columns) else 'X' | |
if right == '.': | |
for i in range(1, dist): | |
right = layout[r][c+i] if c+i in range(columns) else 'X' | |
if right == 'L' or right == '#' or right == 'X': break | |
up = layout[r-1][c] if r-1 in range(rows) else 'X' | |
if up == '.': | |
for i in range(1, dist): | |
up = layout[r-i][c] if r-i in range(columns) else 'X' | |
if up == 'L' or up == '#' or up == 'X': break | |
down = layout[r+1][c] if r+1 in range(rows) else 'X' | |
if down == '.': | |
for i in range(1, dist): | |
down = layout[r+i][c] if r+i in range(rows) else 'X' | |
if down == 'L' or down == '#' or down == 'X': break | |
up_left = layout[r-1][c-1] if r-1 in range(rows) and c-1 in range(columns) else 'X' | |
if up_left == '.': | |
for i in range(1, dist): | |
up_left = layout[r-i][c-i] if r-i in range(rows) and c-i in range(columns) else 'X' | |
if up_left == 'L' or up_left == '#' or up_left == 'X': break | |
up_right = layout[r-1][c+1] if r-1 in range(rows) and c+1 in range(columns) else 'X' | |
if up_right == '.': | |
for i in range(1, dist): | |
up_right = layout[r-i][c+i] if r-i in range(rows) and c+i in range(columns) else 'X' | |
if up_right == 'L' or up_right == '#' or up_right == 'X': break | |
down_left = layout[r+1][c-1] if r+1 in range(rows) and c-1 in range(columns) else 'X' | |
if down_left == '.': | |
for i in range(1, dist): | |
down_left = layout[r+i][c-i] if r+i in range(rows) and c-i in range(columns) else 'X' | |
if down_left == 'L' or down_left == '#' or down_left == 'X': break | |
down_right = layout[r+1][c+1] if r+1 in range(rows) and c+1 in range(columns) else 'X' | |
if down_right == '.': | |
for i in range(1, dist): | |
down_right = layout[r+i][c+i] if r+i in range(rows) and c+i in range(columns) else 'X' | |
if down_right == 'L' or down_right == '#' or down_right == 'X': break | |
adjacent_seats = [left, right, up, down, up_left, up_right, down_left, down_right] | |
taken_seats = 0 | |
for seat in adjacent_seats: | |
if seat == '#': taken_seats += 1 | |
if current == 'L' and taken_seats == 0: current = '#' | |
if current == '#' and taken_seats >= 5: current = 'L' | |
seat_cols.append(current) | |
seats.append(seat_cols) | |
return(seats) | |
while True: | |
os.system('clear') | |
rows, columns = os.popen('stty size', 'r').read().split() | |
layout = generate_layout(int(columns), int(rows)) | |
occupied_seat_counts = [0] | |
seats = model_seats(layout) | |
with Live(refresh_per_second=10, vertical_overflow='visable') as live: | |
while True: | |
time.sleep(.1) | |
seats = model_seats(seats) | |
layout = print_layout(seats) | |
live.update(layout) | |
occupied_seats = layout.count('▓') | |
if occupied_seats == occupied_seat_counts[-1]: | |
break | |
else: | |
occupied_seat_counts.append(occupied_seats) | |
# result = occupied_seat_counts[-1] | |
# print("RESULT: {}".format(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment