-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd4p2_printing_department.py
More file actions
49 lines (44 loc) · 1.8 KB
/
Copy pathd4p2_printing_department.py
File metadata and controls
49 lines (44 loc) · 1.8 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
with open("puzzle_input_day4", "r") as f:
lines = f.read().strip().splitlines()
grid = [list(line) for line in lines]
num_rows = len(grid)
num_cols = len(grid[0])
# Helper function to count neighbours at a single position (a, b)):
def count_neigbours(grid, a, b):
counter = 0
if 0 <= a-1 < num_rows and 0<= b-1 < num_cols and grid[a-1][b-1] == "@":
counter += 1
if 0 <= a < num_rows and 0<= b-1 < num_cols and grid[a][b-1] == "@":
counter += 1
if 0 <= a+1 < num_rows and 0<= b-1 < num_cols and grid[a+1][b-1] == "@":
counter += 1
if 0 <= a-1 < num_rows and 0<= b < num_cols and grid[a-1][b] == "@":
counter += 1
if 0 <= a+1 < num_rows and 0<= b < num_cols and grid[a+1][b] == "@":
counter += 1
if 0 <= a-1 < num_rows and 0<= b+1 < num_cols and grid[a-1][b+1] == "@":
counter += 1
if 0 <= a < num_rows and 0<= b+1 < num_cols and grid[a][b+1] == "@":
counter += 1
if 0 <= a+1 < num_rows and 0<= b+1 < num_cols and grid[a+1][b+1] == "@":
counter += 1
return counter
# Main function to find all accessible rolls:
def find_accessible_rolls(grid):
accessible = []
for a in range(num_rows):
for b in range(num_cols):
if grid[a][b] == "@":
if count_neigbours(grid, a, b) < 4:
accessible.append((a,b))
return accessible
# Loop to find and remove:
total_removed = 0
while True: # Creates a loop that runs forever until told to stop ("break")
accessible = find_accessible_rolls(grid) # Find accessible rolls in current state
if len(accessible) == 0:
break
for (a, b) in accessible:
grid[a][b] = "." # Remove accessible rolls
total_removed += len(accessible)
print(f"Total rolls removed: {total_removed}.")