summaryrefslogtreecommitdiff
path: root/2025/aoc2025-d04.py
blob: 46e7578271d38057650a53fa3302949fb3e08b63 (plain)
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
#advent of code 2025
#day 04
part1=0;
part2=0;
wall=set();
PuzzleInput=open("04.in","r");
adjacent=[(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)];
def IsAccessible(grid,p):
	neighbors=0;
	px,py=p;
	for dx,dy in adjacent:
		neighbors+=(px+dx,py+dy) in grid;
	return neighbors<4;
#parsing
for y,line in enumerate(PuzzleInput):
	for x,c in enumerate(line[:-1]):
		if c=="@": wall.add((x,y));
PuzzleInput.close();
#solution
while True:
	removed=set();
	for pos in wall:
		if IsAccessible(wall,pos):
			removed.add(pos);
	if len(removed)==0: break;
	wall.difference_update(removed);
	if part1==0: part1+=len(removed);
	part2+=len(removed);
#answers
print("part 1",part1);
print("part 2",part2);