From d23375e195c5a7ccbd85ddeb199b60239ce8c838 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Dec 2021 05:36:57 +0000 Subject: day 4: initial draft --- 4.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 4.py diff --git a/4.py b/4.py new file mode 100644 index 0000000..7ad6067 --- /dev/null +++ b/4.py @@ -0,0 +1,32 @@ +from utils import open_day + +nums, *boards = open_day(4).read().split('\n\n') +nums = list(map(int, nums.split(','))) +boards = [[[(int(n), False) for n in l.split()] for l in b.rstrip().split('\n')] for b in boards] + +def win(board): + for y in board: + if all(x[1] for x in y): + return True + for x in range(len(board[0])): + if all(board[y][x][1] for y in range(len(board))): + return True + return False + +won = set() +wins = [] +for n in nums: + for i, b in enumerate(boards): + tot = 0 + for y in range(len(b)): + for x in range(len(b[y])): + if b[y][x][0] == n: + b[y][x] = (b[y][x][0], True) + if b[y][x][1] == False: + tot += b[y][x][0] + if i not in won and win(b): + won.add(i) + wins.append(n * tot) + +print(wins[0]) +print(wins[-1]) -- cgit v1.2.3-54-g00ecf