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
|
#advent of code 2025
#day 07
part1=0;
part2=0;
#parsing
grid=dict();
Ymax=0;
Xmax=0;
PuzzleInput=open("07.in","r");
for y,line in enumerate(PuzzleInput):
Ymax+=1;
for x,c in enumerate(line.replace("\n","")):
grid[(x,y)]=c;
Xmax=max(Xmax,x+1);
if c=="S":
Start=(x,y);
grid[(x,y)]=".";
PuzzleInput.close();
#solution
total=(0,0);
pathing=dict();
pathing[total]=set();
hitBottom=dict();
splitters=set(); #encountered splitters
beams=set([(Start,total)]);
#recursive search
def findTimelines(point):
if point in hitBottom:
return hitBottom[point];
hitBottom[point]=0;
for path in pathing[point]:
hitBottom[point]+=findTimelines(path);
return hitBottom[point];
for step in range(0,Ymax+1,2): #every 2nd line is empty so i'm skipping it
newbeams=set();
while beams:
b,prev=beams.pop();
pathing[prev].add(b);
if b not in pathing: pathing[b]=set();
bx,by=b;
beam=(bx,by+2);
if grid.get(beam,".")=="^":
splitters.add(beam);
newbeams.add(((bx+1,by+2),(bx,by)));
newbeams.add(((bx-1,by+2),(bx,by)));
else:
newbeams.add(((bx,by+2),(bx,by)));
if by+2>Ymax and b not in hitBottom:
hitBottom[b]=1; #register from which points beams hit the bottom
beams=newbeams;
part1=len(splitters);
part2=findTimelines(Start);
print("part 1",part1,part1==1533);
print("part 2",part2,part2==10733529153890);
|