From 56a15dff0a0ef1ed0c864e225e8c0d0c8c0dd8af Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Sun, 29 Dec 2024 01:46:36 +0100 Subject: [PATCH] 2024(8): solve part 1 --- 2024/08/part1.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2024/08/testinput | 12 +++++++++++ 2 files changed, 63 insertions(+) create mode 100644 2024/08/part1.py create mode 100644 2024/08/testinput diff --git a/2024/08/part1.py b/2024/08/part1.py new file mode 100644 index 0000000..bf754df --- /dev/null +++ b/2024/08/part1.py @@ -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))) diff --git a/2024/08/testinput b/2024/08/testinput new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/08/testinput @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............