From 337cabb0ed4c48a612924467111353a518f1365c Mon Sep 17 00:00:00 2001 From: Ben Charlton Date: Fri, 11 Dec 2020 09:29:18 +0000 Subject: [PATCH] day 11 --- 11/11-1.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/11-2.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 11/11-1.py create mode 100644 11/11-2.py diff --git a/11/11-1.py b/11/11-1.py new file mode 100644 index 0000000..f47367f --- /dev/null +++ b/11/11-1.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 + +import sys +import copy + +waiting = [] +for line in sys.stdin: + waiting.append(line.rstrip()) + +def check_seat (waiting, seat_x, seat_y): + seats = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]] + count = 0 + for seat in seats: + x = seat_x + seat[0] + y = seat_y + seat[1] + if x < 0: + continue + if x >= len(waiting[seat_y]): + continue + if y < 0: + continue + if y >= len(waiting): + continue + s = waiting[y][x] + if s == '#': + count = count + 1 + return count + + +seen = {} +stable = 0 +occ = 0 +while True: + newwaiting = [] + occ = 0 + for seat_y in range(len(waiting)): + newrow = '' + for seat_x in range(len(waiting[seat_y])): + count = check_seat(waiting,seat_x,seat_y) + if waiting[seat_y][seat_x] =='.': + newrow = newrow + '.' + elif waiting[seat_y][seat_x] =='#' and count >= 4: + newrow = newrow + 'L' + elif waiting[seat_y][seat_x] =='L' and count == 0: + newrow = newrow + '#' + occ = occ + 1 + else: + newrow = newrow + waiting[seat_y][seat_x] + + if newrow[-1] == '#': + occ = occ + 1 + + newwaiting.append(newrow) + waiting = newwaiting + + pattern = "".join(newwaiting) + if pattern in seen: + break + + seen[pattern] = 1 + stable = stable + 1 +print('1',stable, occ) + diff --git a/11/11-2.py b/11/11-2.py new file mode 100644 index 0000000..bb2fcce --- /dev/null +++ b/11/11-2.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 + +import sys +import copy + +waiting = [] +for line in sys.stdin: + waiting.append(line.rstrip()) + +def check_seat (waiting, seat_x, seat_y): + seats = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]] + count = 0 + for seat in seats: + x = seat_x + seat[0] + y = seat_y + seat[1] + while x >= 0 and x < len(waiting[seat_y]) and y >= 0 and y < len(waiting): + s = waiting[y][x] + if s == 'L': + break + if s == '#': + count = count + 1 + break + x = x + seat[0] + y = y + seat[1] + return count + + +seen = {} +stable = 0 +occ = 0 +while True: + newwaiting = [] + occ = 0 + for seat_y in range(len(waiting)): + newrow = '' + for seat_x in range(len(waiting[seat_y])): + count = check_seat(waiting,seat_x,seat_y) + if waiting[seat_y][seat_x] =='.': + newrow = newrow + '.' + elif waiting[seat_y][seat_x] =='#' and count >= 5: + newrow = newrow + 'L' + elif waiting[seat_y][seat_x] =='L' and count == 0: + newrow = newrow + '#' + occ = occ + 1 + else: + newrow = newrow + waiting[seat_y][seat_x] + + if newrow[-1] == '#': + occ = occ + 1 + + newwaiting.append(newrow) + waiting = newwaiting + + #print("\n".join(waiting)) + #print() + pattern = "".join(newwaiting) + if pattern in seen: + break + + seen[pattern] = 1 + stable = stable + 1 + +print('2',stable, occ) + +