aboutsummaryrefslogtreecommitdiff
path: root/2024/08/puzzles.py
blob: 27efbb0ef2d6f20697feeabc417ab80271eef876 (plain) (blame)
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
42
#!/usr/bin/python3

import collections
import itertools


type Antennas = defaultdict[str, list[complex]]


def main() -> None:
	with open("input", "r") as f:
		data = [x.rstrip() for x in f.readlines()]
	w, h = len(data[0]), len(data)

	antennas: Antennas = collections.defaultdict(list)
	antinodes: set[complex] = set()

	for y, row in enumerate(data):
		for x, ch in enumerate(row):
			if ch != '.':
				antennas[ch].append(complex(x, y))

	for coords in antennas.values():
		for a, b in itertools.combinations(coords, 2):
			v⃗ = a - b
			u⃗ = b - a
			# START PART 1
			antinodes.add(a + u⃗*2)
			antinodes.add(b + v⃗*2)
			# END PART 1 START PART 2
			x = int(w // abs(v⃗.real)) + 1
			y = int(h // abs(v⃗.imag)) + 1
			for i in range(1, min(x, y)):
				antinodes.add(a + u⃗*i)
				antinodes.add(b + v⃗*i)
			# END PART 2

	print(sum(0 <= x.real < w and 0 <= x.imag < h for x in antinodes))


if __name__ == "__main__":
	main()