-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.py
More file actions
71 lines (61 loc) · 2.31 KB
/
Copy pathday04.py
File metadata and controls
71 lines (61 loc) · 2.31 KB
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
#file = open("input/test04.txt")
file = open("input/input04.txt")
grid = []
for line in file:
if line[-1] == '\n':
grid.append(line[:-1])
else:
grid.append(line)
#for row in grid:
# print (row)
#print('')
#returns number of adjacent rolls for given coordinates and input-grid
def checkNeighborhoodRolls(row, col, input):
if (row < 0) or (col < 0) or (row >= len(input[0])) or (col >= len(input)): # requested cell out of bounds
return -1
if not (input[row][col] == '@'): # requested cell has no roll
return -1
count = 0
for i in range(row-1, row+2):
for j in range(col-1, col+2):
if (i < 0) or (j < 0) or (i >= len(input[0])) or (j >= len(input)) or ((i==row) and (j==col)): # neighborhood out of bounds
pass
else:
if (input[i][j] == '@'):
count += 1
return count
# removes accessible roles from a marked input grid, overwrites the grid, returns number of removed items
def removeMarkedRolls(input):
removeNum = 0
for row in range(len(input)):
for col in range(len(input[0])):
if (input[row][col] == 'x'):
input[row] = input[row][0:col] + "." + input[row][col+1:]
removeNum += 1
#print("\nRemove " + str(removeNum) + " rolls.\n")
return removeNum
countAccessibleRolls = 0
markedGrid = grid.copy()
for row in range(len(grid)):
for cell in range(len(grid[0])):
cellStatus = checkNeighborhoodRolls(row, cell, grid)
if (cellStatus >= 0) and (cellStatus < 4):
markedGrid[row] = markedGrid[row][0:cell] + "x" + markedGrid[row][cell+1:]
countAccessibleRolls += 1
#for row in markedGrid:
# print(row)
while(removeMarkedRolls(markedGrid) > 0):
newGrid = markedGrid.copy()
# for row in markedGrid:
# print(row)
for row in range(len(markedGrid)):
for cell in range(len(markedGrid[0])):
cellStatus = checkNeighborhoodRolls(row, cell, markedGrid)
if (cellStatus >= 0) and (cellStatus < 4):
newGrid[row] = newGrid[row][0:cell] + "x" + newGrid[row][cell+1:]
countAccessibleRolls += 1
# print("\nMark new rolls\n")
markedGrid = newGrid
# for row in markedGrid:
# print(row)
print(countAccessibleRolls)