Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. def day11_1():
  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.     layout = {}
  12.     while True:
  13.         for (row, col), seat in old_layout.items():
  14.             if seat == ".":
  15.                 layout[row, col] = "."
  16.             elif seat == "L":
  17.                 neighbors = ""
  18.                 for i in range(-1, 2):
  19.                     for j in range(-1,2):
  20.                         if i == j == 0:
  21.                             continue
  22.                         neighbors += old_layout.get((row-i, col-j), ".")
  23.                 occupied = neighbors.count("#")
  24.  
  25.                 if not occupied:
  26.                     layout[row, col] = "#"
  27.  
  28.             elif seat == "#":
  29.                 neighbors = ""
  30.                 for i in range(-1, 2):
  31.                     for j in range(-1, 2):
  32.                         if i == j == 0:
  33.                             continue
  34.                         neighbors += old_layout.get((row - i, col - j), ".")
  35.                 occupied = neighbors.count("#")
  36.  
  37.                 if occupied >= 4:
  38.                     layout[row, col] = "L"
  39.  
  40.         if layout == old_layout:
  41.             break
  42.         old_layout = {key: value for key, value in layout.items()}
  43.  
  44.     count = 0
  45.     for v in layout.values():
  46.         if v == "#":
  47.             count += 1
  48.  
  49.     return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement