diff options
Diffstat (limited to '2024/aoc2024-d08.py')
-rw-r--r-- | 2024/aoc2024-d08.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/2024/aoc2024-d08.py b/2024/aoc2024-d08.py new file mode 100644 index 0000000..b977a26 --- /dev/null +++ b/2024/aoc2024-d08.py @@ -0,0 +1,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)); |