From 11208c7e33072dbfdb54d9444accc3ad200554fa Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 11 Dec 2020 17:23:29 +1030 Subject: [PATCH] 2020 Day 11 --- 2020/day11.py | 49 ++++++++++++++++++++++++++++ 2020/input11 | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 2020/day11.py create mode 100644 2020/input11 diff --git a/2020/day11.py b/2020/day11.py new file mode 100644 index 0000000..c8cf2e1 --- /dev/null +++ b/2020/day11.py @@ -0,0 +1,49 @@ +import numpy as np +with open('input11', 'r') as file: + seatmap = np.array([[c=='L' for c in line.strip()] for line in file.readlines()]) + +dir_slices = [np.s_[:-1,:], np.s_[:-1,:-1], np.s_[:,:-1], np.s_[1:,:-1], np.s_[1:,:], np.s_[1:,1:], np.s_[:,1:], np.s_[:-1,1:]] + +def sim_step(seatmap, seatfilled): + neighbours = np.zeros_like(seatfilled, dtype=np.int8) + for i, dst in enumerate(dir_slices): + src = dir_slices[(i+4)%8] + neighbours[dst] += seatfilled[src] + output = seatfilled.copy() + output[np.logical_and(seatfilled==True, neighbours>=4)] = False + output[np.logical_and(seatfilled==False, np.logical_and(neighbours==0, seatmap==True))] = True + return output + +occupied_seats = seatmap & False +while(True): + last_occ = occupied_seats.copy() + occupied_seats = sim_step(seatmap, occupied_seats) + if (occupied_seats == last_occ).all(): + print(f'Part 1: Occupied seats = {occupied_seats.sum()}') + break + + +def sim_step_los(seatmap, seatfilled): + neighbours = np.zeros_like(seatfilled, dtype=np.int8) + for i, dst in enumerate(dir_slices): + src = dir_slices[(i+4)%8] + # Make floor propagate sight + fill2 = seatfilled.copy() + while(True): + fill2old = fill2.copy() + fill2[dst][~seatmap[dst]] |= fill2[src][~seatmap[dst]] + if (fill2 == fill2old).all(): + break + neighbours[dst] += fill2[src] + output = seatfilled.copy() + output[np.logical_and(seatfilled==True, neighbours>=5)] = False + output[np.logical_and(seatfilled==False, np.logical_and(neighbours==0, seatmap==True))] = True + return output + +occupied_seats = seatmap & False +while(True): + last_occ = occupied_seats.copy() + occupied_seats = sim_step_los(seatmap, occupied_seats) + if (occupied_seats == last_occ).all(): + print(f'Part 2: Occupied seats = {occupied_seats.sum()}') + break diff --git a/2020/input11 b/2020/input11 new file mode 100644 index 0000000..0e5e219 --- /dev/null +++ b/2020/input11 @@ -0,0 +1,90 @@ +LLLLLLLL.LLL.LLLLLLLLLLLLL.LL.LLLLLL.LL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLLL +LLLLLLLL.LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLL.LL.LLLLLLL.LLLLLL.LLLLLLLLL.LLLL.LLLLL.LLLLLLLL +LLLLLLLL.LLLLL.LLLL.LLL.LLLLLLLLLLLL.LLL..LLLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL +LLLLLLLL.LLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLL.LLLLL.LL.L.LLLLLLLLLLLLLLLL..LLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLL +LLLLLLL.LLLLLLLLLLL.LLLLLLLLL.L.LLLLLLLLLLLLL..LLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLL.LL.LLLLL +..L.L...L....L......LL.LLL..L.L.L.L...L.........LLLL..L..L...LL..L.L.L.....LLLLL..L..L..L.. +LLLLLLLL..LLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLLLLLL.LLL.LLLLLL.LLLLLLLL +LLLLLLLLLLLLLLLLLL..LLLLLLLLL.LLLLLLLLLL.LL.LLLLL.LLL..LLLLLLLLLLL..LLLL.LLLLLLLLL.LLLLLLLL +LLLLLLLLLLLLLL.L.LL.LLLLLLLLL.LLLL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