summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-04 05:36:57 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-04 05:36:57 +0000
commitd23375e195c5a7ccbd85ddeb199b60239ce8c838 (patch)
tree0116e8210308588d4c72d73d9bb0a1ef9e132efa
parentd1ddf896483a8679ef6e33cd207e8a3482513803 (diff)
downloadaoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.tar.gz
aoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.tar.xz
aoc2021-d23375e195c5a7ccbd85ddeb199b60239ce8c838.zip
day 4: initial draft
-rw-r--r--4.py32
1 files changed, 32 insertions, 0 deletions
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])