summaryrefslogtreecommitdiff
path: root/4_2021.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-06-15 22:20:32 -0600
committerJack Sangdahl <[email protected]>2024-06-15 22:20:32 -0600
commit8e313e2614c7a0fe5af924febdbacc650e33835e (patch)
tree63426b6942ceebe17708140c972a7d7451bd2f74 /4_2021.py
downloadaoc-8e313e2614c7a0fe5af924febdbacc650e33835e.tar.gz
aoc-8e313e2614c7a0fe5af924febdbacc650e33835e.zip
intial
Diffstat (limited to '4_2021.py')
-rw-r--r--4_2021.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/4_2021.py b/4_2021.py
new file mode 100644
index 0000000..0f25ad0
--- /dev/null
+++ b/4_2021.py
@@ -0,0 +1,58 @@
+import sys
+
+data = sys.argv[1] if len(sys.argv) > 1 else "../day4data.txt"
+
+numbers = None
+B = []
+F = []
+board = []
+for line in open(data):
+ line = line.strip()
+ if numbers is None:
+ numbers = [int(x) for x in line.split(",")]
+ else:
+ if line:
+ board.append([int(x) for x in line.split()])
+ else:
+ if board:
+ B.append(board)
+ board = []
+B.append(board)
+
+for b in B:
+ assert len(b) == 5 and len(b[0]) == 5
+ F.append([[False for _ in range(5)] for _ in range(5)])
+
+WON = [False for _ in range(len(B))]
+for num in numbers:
+ for i in range(len(B)):
+ for r in range(5):
+ for c in range(5):
+ if B[i][r][c] == num:
+ F[i][r][c] = True
+
+ won = False
+ for r in range(5):
+ ok = True
+ for c in range(5):
+ if not F[i][r][c]:
+ ok = False
+ if ok:
+ won = True
+ for c in range(5):
+ ok = True
+ for r in range(5):
+ if not F[i][r][c]:
+ ok = False
+ if ok:
+ won = True
+ if won and not WON[i]:
+ WON[i] = True
+ nwin = len([j for j in range(len(B)) if WON[j]])
+ if nwin == 1 or nwin == len(B):
+ unmarked = 0
+ for r in range(5):
+ for c in range(5):
+ if not F[i][r][c]:
+ unmarked += B[i][r][c]
+ print(unmarked * num)