Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Dec 11 09:46:47 2020
- @author: drachenmaul
- """
- from copy import deepcopy
- file = open("inputs/Day11.txt", "r")
- data = file.read()
- file.close()
- floorplan = data.split('\n')
- def convert_to_inlist(floor_plan):
- """Converts the read strings to ints, because python strings suck
- -1 -> no chair
- 0 -> empty chair
- 1 -> occupied chair
- """
- linelength = len(floor_plan[0])
- plan_to_int = {
- '.' : -1,
- 'L' : 0,
- '#' : 1
- }
- int_plan = [[0]*linelength for x in range(linelength)]
- for i in range(linelength):
- for j in range(linelength):
- int_plan[i][j] = plan_to_int[floor_plan[i][j]]
- return int_plan
- def occupied_neighbours(floor_plan, x, y):
- occupied = 0
- linelength = len(floor_plan[0])
- for i in range(-1,2):
- if x + i < 0 or x + i >= linelength:
- continue
- for j in range(-1,2):
- if y + j < 0 or y + j >= linelength or (i == j == 0):
- continue
- if floor_plan[x + i][y + j] == 1:
- occupied += 1
- return occupied
- def update_grid(floor_plan):
- linelength = len(floor_plan[0])
- new_plan = deepcopy(floor_plan)
- for x in range(linelength):
- for y in range(linelength):
- typ = floor_plan[x][y]
- if typ == -1:
- new_plan[x][y] = -1
- elif typ == 0 and occupied_neighbours(floor_plan, x, y) == 0:
- new_plan[x][y] = 1
- elif typ == 1 and occupied_neighbours(floor_plan, x, y) >= 4:
- new_plan[x][y] = 0
- return new_plan
- def part_1(floor_plan):
- new_plan = convert_to_inlist(floor_plan)
- old_plan = []
- counter = 0
- while old_plan!=new_plan:
- old_plan = deepcopy(new_plan)
- new_plan = update_grid(old_plan)
- counter += 1
- print("Finished after",counter,"Updates")
- # argh, nested list comprehension
- return sum([value for x in new_plan for value in x if value == 1])
- print(part_1(floorplan))
- def chair_coords(floor_plan):
- """returns list of tupels with chair coordinates"""
- coords = []
- length = len(floor_plan)
- for x in range(length):
- for y in range(length):
- if floor_plan[x][y] == 0:
- coords.append((x,y))
- return coords
- def index_test(size,x,y):
- """check if index is not out of bounds"""
- return 0 <= x < size and 0 <= y < size
- def visible_chairs(floor_plan, chair):
- """Creates a list of visibule chairs from chair position"""
- visible = []
- moves = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)]
- for move in moves:
- for i in range(1,(len(floor_plan))):
- x_ = chair[0]+i*move[0]
- y_ = chair[1]+i*move[1]
- if index_test(len(floor_plan), x_, y_):
- if floor_plan[x_][y_] == 0:
- visible.append((x_,y_))
- break
- return visible
- def visible_chairs_list(floor_plan,chair_plan):
- """returns a list of visible chairs for each chair in the chair_plan
- position in returned list correponds to position in chair_plan"""
- full_list = []
- for chair in chair_plan:
- full_list.append(visible_chairs(floor_plan, chair))
- return full_list
- def visible_occupied(floor_plan,vis_list):
- """Returns number of occupied for a single seat"""
- count = 0
- for seat in vis_list:
- count += floor_plan[seat[0]][seat[1]]
- return count
- def update(floor_plan, chair_list, vis_list):
- """returns a new updated floor plan"""
- new_plan = deepcopy(floor_plan)
- counter = 0
- for chair in chair_list:
- x = chair[0]
- y = chair[1]
- status = floor_plan[x][y]
- if status == 0 and visible_occupied(floor_plan, vis_list[counter]) == 0:
- new_plan[x][y] = 1
- elif status == 1 and visible_occupied(floor_plan, vis_list[counter]) >= 5:
- new_plan[x][y] = 0
- counter += 1
- return new_plan
- def part_2(floor_plan):
- """Updates floor plan until it no longer changes"""
- int_plan = convert_to_inlist(floor_plan)
- chairs = chair_coords(int_plan)
- visibility_list = visible_chairs_list(int_plan, chairs)
- old_list = []
- new_list = deepcopy(int_plan)
- while old_list != new_list:
- old_list = deepcopy(new_list)
- new_list = update(old_list, chairs, visibility_list)
- print(sum([value for x in new_list for value in x if value == 1]))
- part_2(floorplan)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement