add day8 part 2

This commit is contained in:
Dmitry Fedotov
2024-12-27 22:08:19 +03:00
parent 2e18e908e5
commit 19bd3221b3
3 changed files with 75 additions and 26 deletions

View File

@@ -2,63 +2,100 @@ import sys
sys.path.append('../aoclib') sys.path.append('../aoclib')
from aoclib import Input from aoclib import Input
import sys
sys.setrecursionlimit(10**6)
def find_pairs(field): def find_pairs(field):
'''suboptimal (shitty) code'''
pairs = set() pairs = set()
for y in range(len(field)): for y in range(len(field)):
for x in range(len(field[0])): for x in range(len(field[0])):
if field[y][x] == '.': if field[y][x] == '.':
continue continue
for y2 in range(len(field)): for y2 in range(y, len(field)):
for x2 in range(len(field[0])): for x2 in range(len(field[0])):
if x == x2 and y == y2: if x == x2 and y == y2:
continue continue
if field[y][x] == field[y2][x2]: if field[y][x] == field[y2][x2]:
# this is a pair # this is a pair
pairs.add((x, y, x2, y2)) pairs.add(((x, y), (x2, y2)))
return pairs return pairs
def find_antinodes(pair, field): def find_antinodes(pair: tuple[tuple[int]], field):
x, y, x2, y2 = pair p1, p2 = pair
x_diff = abs(x - x2) x_diff = p2[0] - p1[0]
y_diff = abs(y - y2) y_diff = p2[1] - p1[1]
one, two = None, None one, two = (p1[0] - x_diff, p1[1] - y_diff), (p2[0] + x_diff, p2[1] + y_diff)
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 = [] nodes = []
if one and not out_of_bounds(one, field): if out_of_bounds(one, field):
antinodes.append(one) one = None
if two and not out_of_bounds(two, field): if out_of_bounds(two, field):
antinodes.append(two) two = None
return one, two
return antinodes
def out_of_bounds(c, field): 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) return c[0] < 0 or c[1] < 0 or c[0] >= len(field[0]) or c[1] >= len(field)
def solve1(field: list): def solve1(field: list):
pairs = find_pairs(field) pairs = find_pairs(field)
print(pairs)
antinodes = set() antinodes = set()
for p in pairs: for p in pairs:
for anode in find_antinodes(p, field): one, two = find_antinodes(p, field)
antinodes.add(anode) if one:
antinodes.add(one)
if two:
antinodes.add(two)
return len(antinodes)
def solve2(field: list):
pairs = find_pairs(field)
antinodes = set()
for p in pairs:
p1, p2 = p
antinodes.add(p1)
antinodes.add(p2)
one, two = find_antinodes(p, field)
run = True
while run:
_one, _two = None, None
if one:
antinodes.add(one)
_one, _ = find_antinodes((one, p1), field)
if two:
antinodes.add(two)
_, _two = find_antinodes((p2, two), field)
if not _one and not _two:
run = False
continue
if _one:
p1 = one
one = _one
if _two:
p2 = two
two = _two
return len(antinodes) return len(antinodes)
if __name__ == '__main__': if __name__ == '__main__':
#field = Input('input_test.txt').lines_as_lists() #field = Input('input_test2.txt').lines_as_lists()
field = Input('input.txt').lines_as_lists() field = Input('input.txt').lines_as_lists()
# print(find_pairs(field))
#part 1 #part 1
print('part 1:', solve1(field)) print('part 1:', solve1(field))
#part 2 #part 2
print('part 2:', '') print('part 2:', solve2(field))

10
day8/input_test2.txt Normal file
View File

@@ -0,0 +1,10 @@
T....#....
...T......
.T....#...
.........#
..#.......
..........
...#......
..........
....#.....
..........

View File

@@ -115,4 +115,6 @@ The original example now has 34 antinodes, including the antinodes that appear o
Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode? Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode?
Your puzzle answer was 1249.
Both parts of this puzzle are complete! They provide two gold stars: **