summaryrefslogtreecommitdiff
path: root/2024/aoc2024-d08.py
blob: b977a263bbbddf8ad4d5fd78d52842ff157acc88 (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
32
33
34
35
36
37
38
39
40
41
#advent of code 2024
#day 08
#cool
xMin, yMin, xMax, yMax = 0,0,0,0; #map bounds
antennas = {}; #dictionary of antennas
antinodes1 = set(); #antinodes for part 1
antinodes2 = set(); #anitnodes for part 2

f = open("08.in","r");
for y,l in enumerate(f):
	yMax = max(yMax,y);
	for x, c in enumerate(l[:-1]):
		xMax = max(xMax,x);
		if c != ".":
			if antennas.get(c,None) == None:
				antennas[c] = [];
			antennas[c].append((x,y));
f.close();

for antenna in antennas: #for each antenna
	for a1 in antennas[antenna]: #for each instance of that antenna
		for a2 in antennas[antenna]: #for each counterpart of that instance
			if a1 == a2: continue; #antenna can't have the antinode on its own
			a1x, a1y = a1;
			a2x, a2y = a2;
			dx = a2x-a1x;
			dy = a2y-a1y;
			if (xMin <= a1x-dx <= xMax) and (yMin <= a1y-dy <= yMax):
				antinodes1.add((a1x-dx,a1y-dy)); 
			antinodes2.add((a1x,a1y)); 
			while True:
				ax = a1x-dx;
				ay = a1y-dy;
				if not ((xMin <= ax <= xMax) and (yMin <= ay <= yMax)):
					break;
				antinodes2.add((ax,ay)); 
				a1x = ax;
				a1y = ay;		

print("part 1 =", len(antinodes1));
print("part 2 =", len(antinodes2));