-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
53 lines (41 loc) · 1.34 KB
/
day04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
import re
from typing import List
lines = sys.stdin.read().split('\n\n')
numbers = [int(x) for x in lines[0].split(',')]
grids = [[[int(x) for x in re.split('\s+', line.strip())]
for line in grid.strip().split('\n')] for grid in lines[1:]]
def isGridWinning(grid: List[List[int]]):
# First check rows
for line in grid:
if sum(line) == -5:
return True
# Then check cols
for col in range(5):
if sum((grid[i][col] for i in range(5))) == -5:
return True
return False
def mark(grid: List[List[int]], x: int):
for j in range(5):
for i in range(5):
if grid[j][i] == x:
grid[j][i] = -1
def score(num, grid):
sumOfGrid = sum(x for line in grid for x in line if x != -1)
return num * sumOfGrid
def main():
firstFound = False
notWon = set(range(len(grids)))
for num in numbers:
for i, grid in enumerate(grids):
if i in notWon:
mark(grid, num)
if isGridWinning(grid):
if not firstFound:
print('Part 1:', score(num, grid))
firstFound = True
if len(notWon) == 1:
print('Part 2:', score(num, grid))
return
notWon.remove(i)
main()