diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-08 13:47:50 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-08 13:47:50 +0100 |
commit | 6645309667a831ffcf27e0a9ef2b4c5ade60bd1d (patch) | |
tree | b451886a2fd6953fd8af2c139bf1741039d72d57 /2024/08/puzzles.py | |
parent | f891c9ed5cd9b0f5d112ef50fcb811789cc5d0cf (diff) |
Add 2024 day 8 solutions
Diffstat (limited to '2024/08/puzzles.py')
-rw-r--r-- | 2024/08/puzzles.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/2024/08/puzzles.py b/2024/08/puzzles.py new file mode 100644 index 0000000..87c4c46 --- /dev/null +++ b/2024/08/puzzles.py @@ -0,0 +1,40 @@ +#!/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 + for i in range(1, max(w, h)): + 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()
\ No newline at end of file |