2024(8): easy peasy

This commit is contained in:
2024-12-29 01:59:41 +01:00
parent 56a15dff0a
commit 74cb600d69
2 changed files with 92 additions and 0 deletions

View File

@ -89,3 +89,45 @@ The first example has antennas with two different frequencies, so the antinodes
Because the topmost `A`\-frequency antenna overlaps with a `0`\-frequency antinode, there are _`14`_ total unique locations that contain an antinode within the bounds of the map.
Calculate the impact of the signal. _How many unique locations within the bounds of the map contain an antinode?_
### Part Two
Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations.
Whoops!
After updating your model, it turns out that an antinode occurs at _any grid position_ exactly in line with at least two antennas of the same frequency, regardless of distance. This means that some of the new antinodes will occur at the position of each antenna (unless that antenna is the only one of its frequency).
So, these three `T`\-frequency antennas now create many antinodes:
T....#....
...T......
.T....#...
.........#
..#.......
..........
...#......
..........
....#.....
..........
In fact, the three `T`\-frequency antennas are all exactly in line with two antennas, so they are all also antinodes! This brings the total number of antinodes in the above example to _`9`_.
The original example now has _`34`_ antinodes, including the antinodes that appear on every antenna:
##....#....#
.#.#....0...
..#.#0....#.
..##...0....
....0....#..
.#...#A....#
...#..#.....
#....#.#....
..#.....A...
....#....A..
.#........#.
...#......##
Calculate the impact of the signal using this updated model. _How many unique locations within the bounds of the map contain an antinode?_

50
2024/08/part2.py Normal file
View File

@ -0,0 +1,50 @@
import numpy as np
from part1 import *
verbose = False
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]
antinodes += [a, b]
diff_a = a - b
diff_b = b - a
# TODO: Is copy needed?
overtone = a.copy() + diff_a
while isValid(m.shape, overtone):
antinodes.append(overtone.copy())
overtone += diff_a
overtone = a.copy() - diff_a
while isValid(m.shape, overtone):
antinodes.append(overtone.copy())
overtone -= diff_a
overtone = b.copy() + diff_b
while isValid(m.shape, overtone):
antinodes.append(overtone.copy())
overtone += diff_b
overtone = b.copy() - diff_b
while isValid(m.shape, overtone):
antinodes.append(overtone.copy())
overtone -= diff_b
print(len(np.unique(antinodes,axis=0)))