-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4_part2.py
105 lines (93 loc) · 2.45 KB
/
day4_part2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
def checkBoard(flag, winNum):
for b, board in enumerate(boards):
for r, row in enumerate(board):
counter = 0
for n, number in enumerate(row):
if number == "X":
counter +=1
if counter == 5:
if flag == True:
winningBoards.append(b) if b not in winningBoards else True
else:
calculate(board,winNum)
exit()
for b, board in enumerate(boards):
for r, row in enumerate(board):
counter = 0
for n, number in enumerate(row):
if board[n][r] == "X":
counter += 1
if counter ==5:
if flag ==True:
winningBoards.append(b) if b not in winningBoards else True
else:
calculate(board,winNum)
exit()
def calculate(board, winNum):
score = 0
for r, row in enumerate(board):
for n, number in enumerate(row):
if number != "X":
score += int(number)
print(score, winNum)
print(int(score)* int(winNum))
if __name__ == "__main__":
nums = []
boards = []
winningBoards = []
with open('day4-input','r') as f:
nums = f.readline().split(',')
for i, line in enumerate(f.readlines()):
if len(line) == 1:
boards.append([[]])
else:
rows = line.split('\n')
for row in rows:
boards[-1].append([])
numsInBoard = row.split(' ')
for item in numsInBoard:
item = item.strip(' ')
if item.isdigit():
boards[-1][-1].append(item)
#Clean boards
for i, board in enumerate(boards):
for x, row in enumerate(board):
if len(row) == 0:
del boards[i][x]
for num in nums:
for b, board in enumerate(boards):
for r, row in enumerate(board):
for n, number in enumerate(row):
if num == number:
boards[b][r][n] = "X"
checkBoard(True, number)
print(winningBoards)
print("Again")
boards =[]
#Load data again
with open('day4.dat','r') as f:
nums = f.readline().split(',')
for i, line in enumerate(f.readlines()):
if len(line) == 1:
boards.append([[]])
else:
rows = line.split('\n')
for row in rows:
boards[-1].append([])
numsInBoard = row.split(' ')
for item in numsInBoard:
item = item.strip(' ')
if item.isdigit():
boards[-1][-1].append(item)
#Clean boards
for i, board in enumerate(boards):
for x, row in enumerate(board):
if len(row) == 0:
del boards[i][x]
for num in nums:
for r, row in enumerate(boards[winningBoards[-1]]):
print(num, row)
for n, number in enumerate(row):
if num == number:
boards[winningBoards[-1]][r][n] = "X"
checkBoard(False, number)