add day 8 part 2

This commit is contained in:
Dmitry Fedotov
2024-12-16 22:49:28 +03:00
parent c11a5ed460
commit 2e18e908e5
4 changed files with 244 additions and 0 deletions

64
day8/code.py Normal file
View File

@@ -0,0 +1,64 @@
import sys
sys.path.append('../aoclib')
from aoclib import Input
def find_pairs(field):
'''suboptimal (shitty) code'''
pairs = set()
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == '.':
continue
for y2 in range(len(field)):
for x2 in range(len(field[0])):
if x == x2 and y == y2:
continue
if field[y][x] == field[y2][x2]:
# this is a pair
pairs.add((x, y, x2, y2))
return pairs
def find_antinodes(pair, field):
x, y, x2, y2 = pair
x_diff = abs(x - x2)
y_diff = abs(y - y2)
one, two = None, None
if x2 >= x and y2 >= y:
one = (y-y_diff, x-x_diff)
two = (y2+y_diff, x2+x_diff)
elif x2 >= x and y2 < y:
one = (y+y_diff, x-x_diff)
two = (y2-y_diff, x2+x_diff)
antinodes = []
if one and not out_of_bounds(one, field):
antinodes.append(one)
if two and not out_of_bounds(two, field):
antinodes.append(two)
return antinodes
def out_of_bounds(c, field):
return c[0] < 0 or c[1] < 0 or c[0] >= len(field[0]) or c[1] >= len(field)
def solve1(field: list):
pairs = find_pairs(field)
print(pairs)
antinodes = set()
for p in pairs:
for anode in find_antinodes(p, field):
antinodes.add(anode)
return len(antinodes)
if __name__ == '__main__':
#field = Input('input_test.txt').lines_as_lists()
field = Input('input.txt').lines_as_lists()
#part 1
print('part 1:', solve1(field))
#part 2
print('part 2:', '')

50
day8/input.txt Normal file
View File

@@ -0,0 +1,50 @@
...............e...........j6.....................
.....1...............................t.....i......
.....4.......3..............x..tL......m..........
.......L.....................Dxj..................
4....X..................F.....................m...
.............4.......x....F........k..............
......3...................t..........i.........Z..
....L..................y.....F..e.....Z...........
X.............1........C..........i...D...........
........4.....................D.....k.X...m.......
...1...............D........e......6..............
...3.Y...................................m8.......
..OL.........................x....Z....g..........
....3......5.........................6j...........
...................J..5r.F..k...y.................
.......................................Z..a.......
...........................5........j.........a.u.
...p..............Y....X..........................
...O.........................kd...................
........................t.................i.......
..................J..............u...........z....
.O.....9.............J..............p..u..........
.....9............................................
l...6.....1........e......I................a......
...................................az.............
........M.......J...................gI....z.......
.......Y...l...........p......g....d.......W......
........5l....9................d.....g............
.A....9.l.Y............I..............B.......s...
..................................K.....B.........
....M.............7.......8..........h.....K......
.......0f...oc..............G...d7.......z...s..yW
...M........0...........Gf.....................T..
................r......G..................w....h..
...........cP................G.8.R..............T.
.................A.............N............u..B..
..H.c..b............................K...CB.....y..
......c...bP...2............7..K..................
......b.o....0.......P.............s........h.R...
......2........f..S........8.....................R
U....2..............p..............7..............
.HE..b......A.............N..............w....C...
................................N.............w...
.........E...........M................W.......T...
......E...rS2...........W....................N....
.....SP..n.....r..0...............................
.....H..............A............................w
..........n..U....................s...............
..n.So.....U................f.....................
Ho................................................

12
day8/input_test.txt Normal file
View File

@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

118
day8/task.txt Normal file
View File

@@ -0,0 +1,118 @@
--- Day 8: Resonant Collinearity ---
You find yourselves on the roof of a top-secret Easter Bunny installation.
While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!
Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
So, for these two antennas with frequency a, they create the two antinodes marked with #:
..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........
Antennas with different frequencies don't create antinodes; A and a count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a-frequency antinode at its location:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......A...
..........
..........
The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A-frequency antenna:
......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
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?
Your puzzle answer was 361.
The first half of this puzzle is complete! It provides one gold star: *
--- 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?