52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
|
|
def get_neighbours(layout, x, y):
|
|
|
|
neighbours = 0
|
|
for dx in [-1,0,1]:
|
|
for dy in [-1,0,1]:
|
|
if dx == dy == 0:
|
|
continue
|
|
|
|
# Skip if we are outside of bounds
|
|
if not 0 <= y+dy < len(layout):
|
|
continue
|
|
if not 0 <= x+dx < len(layout[y+dy]):
|
|
continue
|
|
|
|
|
|
if layout[y+dy][x+dx] != ".":
|
|
neighbours += 1
|
|
return neighbours
|
|
|
|
|
|
|
|
def walk_map(layout):
|
|
|
|
free_roles = 0
|
|
for y in range(0,len(layout)):
|
|
for x in range(0, len(layout[y])):
|
|
if layout[y][x] != ".":
|
|
|
|
neighbours = get_neighbours(layout, x, y)
|
|
|
|
if neighbours < 4:
|
|
free_roles += 1
|
|
return free_roles
|
|
|
|
|
|
def read_input(filename):
|
|
layout = []
|
|
for line in open(filename).read().splitlines():
|
|
layout.append(list(line))
|
|
return layout
|
|
|
|
# filename = "day04/example_input" # 13
|
|
filename = "day04/input" # 1384
|
|
layout = read_input(filename)
|
|
|
|
rolls = walk_map(layout)
|
|
|
|
|
|
|
|
print(f"Total free rolls: {rolls}")
|