2024 Dec 08th
This commit is contained in:
parent
fbb492e3ae
commit
5b01fc0fbf
1 changed files with 47 additions and 0 deletions
47
Python/2024/08/main.py
Normal file
47
Python/2024/08/main.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue