Advertisement
Guest User

AoC Day 11 Python 3

a guest
Dec 12th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # day11
  2. from numpy import *
  3. import time
  4. from copy import copy, deepcopy
  5. import numpy.ma as ma
  6.  
  7. input = "d11_data.txt"
  8. test = "d11_test.txt"
  9. round0 = "d11_test0.txt"
  10. round1 = "d11_test1.txt"
  11. round2 = "d11_test2.txt"
  12. round3 = "d11_test3.txt"
  13.  
  14. def read_file(filename):
  15.     with open(filename) as f:
  16.         return f.read().splitlines()
  17.  
  18. #change input data here    
  19. data = read_file(test)
  20.  
  21.  
  22. # Changing the input from L,#,. to 0,1,-1 for easy calculations later
  23. def clean_input(data):
  24.     clean_data=[]
  25.     def cleanstuff(indata):
  26.         for n in indata:
  27.             if n == 'L': return 0
  28.             elif n == '#': return 1
  29.             elif n == '.': return -1
  30.     for line in data:
  31.             line = list(map(cleanstuff,line))
  32.             clean_data.append(line)
  33.     return clean_data
  34.  
  35.  
  36. #Converting datastructure to something more useful and adding a border of -1s to make floating window work easier
  37. def get_matrix(data):
  38.     area = []
  39.     newlist = [-1 for j in range(len(data[0])+2)]
  40.     area.append(newlist)
  41.     i=1
  42.     for line in data:
  43.         area.append([])
  44.         area[i].append(-1)
  45.         for space in line:
  46.             area[i].append(space)
  47.         area[i].append(-1)
  48.         i+=1
  49.     area.append(newlist)
  50.     m_area = reshape(area,((len(area)),len(area[0])))
  51.     return m_area
  52.  
  53.  
  54. #Build a floating window of 3x3 to check surroundings
  55. def build_lookup(x,y):
  56.     lookup = []
  57.     lookup.append([(x-1,y-1),(x-1,y),(x-1,y+1)])
  58.     lookup.append([(x,y-1),(None,None),(x,y+1)])
  59.     lookup.append([(x+1,y-1),(x+1,y),(x+1,y+1)])
  60.     return lookup
  61.  
  62.  
  63. #calculating the sum of occupied places around a given index for a given matrix
  64. def get_sum(lookup, input):
  65.     lookup_sum = 0
  66.     for i in lookup:
  67.         for j in i:
  68.             if (j != (None,None)):
  69.                 val=int(input[j[0]][j[1]])
  70.                 if val >=0: lookup_sum+=val
  71.     return lookup_sum
  72.  
  73.  
  74. #iterating through the input matrix and changing seats for the output matrix
  75. def calculate_round(round_data):
  76.     newround = deepcopy(round_data)
  77.     r=1
  78.     for row in round_data[1:-1]:
  79.         c=1
  80.         for column in round_data[1:-1]:
  81.             surround_count = get_sum(build_lookup(r,c),round_data)
  82.             if (surround_count >= 4 and round_data[r][c]==1): newround[r][c]=0
  83.             elif (surround_count == 0 and round_data[r][c]==0): newround[r][c]=1
  84.             c+=1
  85.         r+=1
  86.     return newround
  87.  
  88.  
  89. #recursive run through matrix inputs, until stable state is reached
  90. def run_rounds(floorplan):
  91.     result=calculate_round(floorplan)
  92.     if ((result==floorplan).all()):
  93.         return result
  94.     return run_rounds(result)
  95.  
  96.  
  97. #start program, and sum up all seats that are occupied
  98. seats = get_matrix(clean_input(data))
  99. result_seats = run_rounds(seats)
  100. masked_result = ma.masked_values(result_seats,-1)
  101. print(masked_result.sum())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement