AdventOfCode/Python/2024/08/main.py

48 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-12-10 09:04:15 -08:00
from itertools import combinations
input = open("input", 'r')
# d = [line for line in input]
# d = [[val for val in line.strip()] for line in input]
d = {}
h = 0
w = 0
for y,line in enumerate(input):
h = max(h,y)
for x,c in enumerate(line.strip()):
w = max(w, x)
if c == ".":
continue
if c in d:
d[c].append(complex(x,y))
else:
d[c] = [complex(x,y)]
h += 1
w += 1
def run(p2=False):
chk = lambda cmp: (0 <= cmp.real < w) and (0 <= cmp.imag < h)
antinodes = set()
for fr,frp in d.items():
for frp1, frp2 in combinations(frp,2):
dif = frp1 - frp2
if not p2 and chk(frp1+dif):
antinodes.add(frp1+dif)
if not p2 and chk(frp2-dif):
antinodes.add(frp2-dif)
frp = frp1
while p2 and chk(frp):
antinodes.add(frp)
frp += dif
frp = frp1
while p2 and chk(frp):
antinodes.add(frp)
frp -= dif
return len(antinodes)
print(run())
print(run(True))