diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 05:36:57 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 05:36:57 +0000 |
commit | d23375e195c5a7ccbd85ddeb199b60239ce8c838 (patch) | |
tree | 0116e8210308588d4c72d73d9bb0a1ef9e132efa | |
parent | d1ddf896483a8679ef6e33cd207e8a3482513803 (diff) | |
download | aoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.tar.gz aoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.tar.xz aoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.zip |
day 4: initial draft
-rw-r--r-- | 4.py | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -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]) |