-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
95 lines (77 loc) · 2.53 KB
/
day4.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
import adventutils
def part1(file):
print("Part 1")
game = adventutils.file_contents(file)
calls = game[0].split(",")
boards = getboards(game)
for num in calls:
call(boards, num)
board, row, column = validate(boards)
if board is not None:
print(score_unmarked(board, num))
break
def part2(file):
print("Part 2")
game = adventutils.file_contents(file)
calls = game[0].split(",")
boards = getboards(game)
for num in calls:
call(boards, num)
board, row, column = validate(boards)
while board is not None:
if board is not None and len(boards) > 1:
boards.remove(board)
board, row, column = validate(boards)
elif board is not None and len(boards) == 1:
print(score_unmarked(board, num))
return
def getboards(game):
boards = list()
current_board = list()
for index in range(2, len(game)):
if game[index] == "":
boards.append(current_board)
current_board = list()
continue
nums = game[index].split(" ")
current_board.append([num for num in nums if num != ""])
return boards
def call(boards, num):
for board in boards:
for row in range(len(board)):
for column in range(len(board[row])):
if board[row][column] == num:
board[row][column] += "x"
def validate(boards):
for board in boards:
for row in range(len(board)):
if validate_row(board, row):
return board, row, None
for column in range(len(board[0])):
if validate_column(board, column):
return board, None, column
return None, None, None
def validate_row(board, row):
if board[row][0].endswith("x"):
for column in range(len(board[row])):
if not board[row][column].endswith("x"):
return False
return True
return False
def validate_column(board, column):
if board[0][column].endswith("x"):
for row in range(len(board)):
if not board[row][column].endswith("x"):
return False
return True
return False
def score_unmarked(board, last_called):
total = 0
for row in board:
for column in row:
if not column.endswith("x"):
total += int(column)
return total * int(last_called)
if __name__ == "__main__":
part1("./data/day4part1.txt")
part2("./data/day4part1.txt")