2024(8): solve part 1

This commit is contained in:
2024-12-29 01:46:36 +01:00
parent 5501195a0b
commit 56a15dff0a
2 changed files with 63 additions and 0 deletions

51
2024/08/part1.py Normal file
View File

@ -0,0 +1,51 @@
import numpy as np
verbose = False
def load_map(filename="input"):
"""
Reads a map from a file without verifying its contents.
"""
with open(filename, "r") as fp:
data = fp.read().splitlines()
chararray = np.array(data, dtype=str)\
.view("U1")\
.reshape((len(data), -1))
return chararray
def isValid(shape: tuple, index: tuple):
for i in range(len(shape)):
if not (0 <= index[i] < shape[i]):
return False
return True
if __name__ == "__main__":
m = load_map()
frequencies = np.unique(m)
antinodes = []
for f in frequencies:
# Emptiness is killing me.
if f == ".":
continue
# Find all antennas with the same frequency.
antennas = np.array(np.where(m == f)).T
# For each pair of antennas, find antinodes.
for i in range(len(antennas)):
for j in range(i + 1, len(antennas)):
a = antennas[i]
b = antennas[j]
c = a + (a - b)
d = b + (b - a)
if isValid(m.shape, c):
antinodes.append(c)
if isValid(m.shape, d):
antinodes.append(d)
print(len(np.unique(antinodes,axis=0)))