summaryrefslogtreecommitdiff
path: root/2024/day8/solve.py
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-12-08 11:05:12 +0100
committerBotond Hende <nettingman@gmail.com>2024-12-08 11:05:12 +0100
commitcbf5c348db4a693b15e455a23e07072587edf4b0 (patch)
treee57080af661a36586623f6f1cd16814f8681a636 /2024/day8/solve.py
parent7b4ce352606bb3edc99203444a19cc844b6bd5a3 (diff)
2024 day8
Diffstat (limited to '2024/day8/solve.py')
-rw-r--r--2024/day8/solve.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/2024/day8/solve.py b/2024/day8/solve.py
new file mode 100644
index 0000000..9dba99e
--- /dev/null
+++ b/2024/day8/solve.py
@@ -0,0 +1,34 @@
+lines = []
+
+with open("input") as f:
+ for line in f:
+ lines.append(line.strip())
+
+antennas: dict[str, list[tuple[int, int]]] = {}
+for row_idx, line in enumerate(lines):
+ for col_idx, char in enumerate(line):
+ if char != ".":
+ if char not in antennas:
+ antennas[char] = []
+
+ antennas[char].append((row_idx, col_idx))
+
+row_count = len(lines)
+col_count = len(lines[0])
+antinodes: set[tuple[int, int]] = set()
+for antenna_list in antennas.values():
+ for idx, antenna1 in enumerate(antenna_list):
+ for antenna2 in antenna_list[idx + 1:]:
+ row_diff = antenna2[0] - antenna1[0]
+ col_diff = antenna2[1] - antenna1[1]
+
+ pos1 = (antenna1[0] - row_diff, antenna1[1] - col_diff)
+ pos2 = (antenna2[0] + row_diff, antenna2[1] + col_diff)
+
+ if 0 <= pos1[0] < row_count and 0 <= pos1[1] < col_count:
+ antinodes.add(pos1)
+
+ if 0 <= pos2[0] < row_count and 0 <= pos2[1] < col_count:
+ antinodes.add(pos2)
+
+print(len(antinodes)) \ No newline at end of file