From 38160d02f3d3f189021a4bf19596f522c3220235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 11 Dec 2020 07:34:30 +0100 Subject: common read --- 20/py/d11.py | 47 +++++++++++------------------------------------ 1 file changed, 11 insertions(+), 36 deletions(-) (limited to '20/py') diff --git a/20/py/d11.py b/20/py/d11.py index 5d5e0c5..048367c 100644 --- a/20/py/d11.py +++ b/20/py/d11.py @@ -3,24 +3,17 @@ import aoc20 import sys -def vis(board, w, h): - s = "" +def read(lines): + board = {} + h = len(lines) + w = len(lines[0]) for y in range(h): for x in range(w): - p = (x, y) - if p in board: - if board[p] == -1: - s += "." - elif board[p] == 0: - s += "L" - elif board[p] == 1: - s += "#" - else: - s += "?" - else: - s += "?" - s += "\n" - return s + if lines[y][x] == "L": + board[(x, y)] = 0 + elif lines[y][x] == ".": + board[(x, y)] = -1 + return board, w, h def amount_neighbours_occ(p, board): @@ -36,16 +29,7 @@ def amount_neighbours_occ(p, board): def pt1(_in): - board = {} - h = len(_in) - w = len(_in[0]) - for y in range(h): - for x in range(w): - if _in[y][x] == "L": - board[(x, y)] = 0 - elif _in[y][x] == ".": - board[(x, y)] = -1 - + board, w, h = read(_in) while True: prev_board = board.copy() for p, state in prev_board.items(): @@ -86,16 +70,7 @@ def amount_in_sight_occ(p, board, w, h): def pt2(_in): - board = {} - h = len(_in) - w = len(_in[0]) - for y in range(h): - for x in range(w): - if _in[y][x] == "L": - board[(x, y)] = 0 - elif _in[y][x] == ".": - board[(x, y)] = -1 - + board, w, h = read(_in) while True: prev_board = board.copy() for p, state in prev_board.items(): -- cgit v1.2.1