Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Dec 11 09:46:47 2020
  4.  
  5. @author: drachenmaul
  6. """
  7.  
  8. from copy import deepcopy
  9.  
  10. file = open("inputs/Day11.txt", "r")
  11. data = file.read()
  12. file.close()
  13. floorplan = data.split('\n')
  14.  
  15. def convert_to_inlist(floor_plan):
  16.     """Converts the read strings to ints, because python strings suck
  17.    -1 -> no chair
  18.    0 -> empty chair
  19.    1 -> occupied chair
  20.    """
  21.  
  22.     linelength = len(floor_plan[0])
  23.     plan_to_int = {
  24.         '.' : -1,
  25.         'L' : 0,
  26.         '#' : 1
  27.         }
  28.     int_plan = [[0]*linelength for x in range(linelength)]
  29.  
  30.     for i in range(linelength):
  31.         for j in range(linelength):
  32.             int_plan[i][j] = plan_to_int[floor_plan[i][j]]
  33.  
  34.     return int_plan
  35.  
  36. def occupied_neighbours(floor_plan, x, y):
  37.     occupied = 0
  38.     linelength = len(floor_plan[0])
  39.     for i in range(-1,2):
  40.         if x + i < 0 or x + i >= linelength:
  41.             continue
  42.         for j in range(-1,2):
  43.             if y + j < 0 or y + j >= linelength or (i == j == 0):
  44.                 continue
  45.             if floor_plan[x + i][y + j] == 1:
  46.                 occupied += 1
  47.     return occupied
  48.  
  49.  
  50. def update_grid(floor_plan):
  51.     linelength = len(floor_plan[0])
  52.     new_plan = deepcopy(floor_plan)
  53.  
  54.     for x in range(linelength):
  55.         for y in range(linelength):
  56.             typ = floor_plan[x][y]
  57.             if typ == -1:
  58.                 new_plan[x][y] = -1
  59.             elif typ == 0 and occupied_neighbours(floor_plan, x, y) == 0:
  60.                 new_plan[x][y] = 1
  61.             elif typ == 1 and occupied_neighbours(floor_plan, x, y) >= 4:
  62.                 new_plan[x][y] = 0
  63.     return new_plan
  64.  
  65.  
  66.  
  67. def part_1(floor_plan):
  68.     new_plan = convert_to_inlist(floor_plan)
  69.     old_plan = []
  70.     counter = 0
  71.     while old_plan!=new_plan:
  72.         old_plan = deepcopy(new_plan)
  73.         new_plan = update_grid(old_plan)
  74.         counter += 1
  75.     print("Finished after",counter,"Updates")
  76.     # argh, nested list comprehension
  77.     return sum([value for x in new_plan for value in x if value == 1])
  78.  
  79.  
  80. print(part_1(floorplan))
  81.  
  82. def chair_coords(floor_plan):
  83.     """returns list of tupels with chair coordinates"""
  84.     coords = []
  85.     length = len(floor_plan)
  86.     for x in range(length):
  87.         for y in range(length):
  88.             if floor_plan[x][y] == 0:
  89.                 coords.append((x,y))
  90.     return coords
  91.  
  92. def index_test(size,x,y):
  93.     """check if index is not out of bounds"""
  94.     return 0 <= x < size and 0 <= y < size
  95.  
  96.  
  97. def visible_chairs(floor_plan, chair):
  98.     """Creates a list of visibule chairs from chair position"""
  99.     visible = []
  100.     moves = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)]
  101.     for move in moves:
  102.         for i in range(1,(len(floor_plan))):
  103.             x_ = chair[0]+i*move[0]
  104.             y_ = chair[1]+i*move[1]
  105.             if index_test(len(floor_plan), x_, y_):
  106.                 if floor_plan[x_][y_] == 0:
  107.                     visible.append((x_,y_))
  108.                     break
  109.  
  110.     return visible
  111.  
  112. def visible_chairs_list(floor_plan,chair_plan):
  113.     """returns a list of visible chairs for each chair in the chair_plan
  114.    position in returned list correponds to position in chair_plan"""
  115.     full_list = []
  116.     for chair in chair_plan:
  117.         full_list.append(visible_chairs(floor_plan, chair))
  118.  
  119.  
  120.  
  121.     return full_list
  122.  
  123. def visible_occupied(floor_plan,vis_list):
  124.     """Returns number of occupied for a single seat"""
  125.     count = 0
  126.     for seat in vis_list:
  127.         count += floor_plan[seat[0]][seat[1]]
  128.     return count
  129.  
  130. def update(floor_plan, chair_list, vis_list):
  131.     """returns a new updated floor plan"""
  132.     new_plan = deepcopy(floor_plan)
  133.     counter = 0
  134.     for chair in chair_list:
  135.         x = chair[0]
  136.         y = chair[1]
  137.         status = floor_plan[x][y]
  138.         if status == 0 and visible_occupied(floor_plan, vis_list[counter]) == 0:
  139.             new_plan[x][y] = 1
  140.         elif status == 1 and visible_occupied(floor_plan, vis_list[counter]) >= 5:
  141.             new_plan[x][y] = 0
  142.         counter += 1
  143.  
  144.  
  145.     return new_plan
  146.  
  147. def part_2(floor_plan):
  148.     """Updates floor plan until it no longer changes"""
  149.     int_plan = convert_to_inlist(floor_plan)
  150.     chairs = chair_coords(int_plan)
  151.     visibility_list = visible_chairs_list(int_plan, chairs)
  152.     old_list = []
  153.     new_list = deepcopy(int_plan)
  154.  
  155.  
  156.     while old_list != new_list:
  157.         old_list = deepcopy(new_list)
  158.         new_list = update(old_list, chairs, visibility_list)
  159.  
  160.     print(sum([value for x in new_list for value in x if value == 1]))
  161.  
  162. part_2(floorplan)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement