Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. def day11_2():
  2.     with open("input11.txt", "r") as file:
  3.         data = file.read()
  4.     data = data.split("\n")
  5.  
  6.     old_layout = {}
  7.     for i, row in enumerate(data):
  8.         for j, seat in enumerate(row):
  9.             old_layout[i, j] = seat
  10.  
  11.     def is_occupied(r, c, direction):
  12.         while True:
  13.             r = r + direction[0]
  14.             c = c + direction[1]
  15.  
  16.             try:
  17.                 if old_layout[r, c] == "#":
  18.                     return 1
  19.                 elif old_layout[r, c] == "L":
  20.                     return 0
  21.             except KeyError:
  22.                 return 0
  23.  
  24.     layout = {}
  25.     while True:
  26.         for (row, col), seat in old_layout.items():
  27.             if seat == ".":
  28.                 layout[row, col] = "."
  29.             elif seat == "L":
  30.                 occupied = 0
  31.                 for i in range(-1, 2):
  32.                     for j in range(-1, 2):
  33.                         if i == j == 0:
  34.                             continue
  35.                         occupied += is_occupied(row, col, (i, j))
  36.  
  37.                 if not occupied:
  38.                     layout[row, col] = "#"
  39.  
  40.             elif seat == "#":
  41.                 occupied = 0
  42.                 for i in range(-1, 2):
  43.                     for j in range(-1, 2):
  44.                         if i == j == 0:
  45.                             continue
  46.                         occupied += is_occupied(row, col, (i, j))
  47.  
  48.                 if occupied >= 5:
  49.                     layout[row, col] = "L"
  50.  
  51.         if layout == old_layout:
  52.             break
  53.         old_layout = {key: value for key, value in layout.items()}
  54.  
  55.     count = 0
  56.     for v in layout.values():
  57.         if v == "#":
  58.             count += 1
  59.  
  60.     return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement