From 5b01fc0fbf270152298870954814219e6ed6a9e1 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Tue, 10 Dec 2024 09:04:15 -0800 Subject: [PATCH] 2024 Dec 08th --- Python/2024/08/main.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Python/2024/08/main.py diff --git a/Python/2024/08/main.py b/Python/2024/08/main.py new file mode 100644 index 0000000..d51eefb --- /dev/null +++ b/Python/2024/08/main.py @@ -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))