2024-12-16 22:49:28 +03:00
|
|
|
import sys
|
|
|
|
sys.path.append('../aoclib')
|
|
|
|
|
|
|
|
from aoclib import Input
|
2024-12-27 22:08:19 +03:00
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.setrecursionlimit(10**6)
|
2024-12-16 22:49:28 +03:00
|
|
|
|
|
|
|
def find_pairs(field):
|
|
|
|
pairs = set()
|
|
|
|
for y in range(len(field)):
|
|
|
|
for x in range(len(field[0])):
|
|
|
|
if field[y][x] == '.':
|
|
|
|
continue
|
2024-12-27 22:08:19 +03:00
|
|
|
for y2 in range(y, len(field)):
|
2024-12-16 22:49:28 +03:00
|
|
|
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
|
2024-12-27 22:08:19 +03:00
|
|
|
pairs.add(((x, y), (x2, y2)))
|
2024-12-16 22:49:28 +03:00
|
|
|
return pairs
|
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
def find_antinodes(pair: tuple[tuple[int]], field):
|
|
|
|
p1, p2 = pair
|
|
|
|
x_diff = p2[0] - p1[0]
|
|
|
|
y_diff = p2[1] - p1[1]
|
2024-12-16 22:49:28 +03:00
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
one, two = (p1[0] - x_diff, p1[1] - y_diff), (p2[0] + x_diff, p2[1] + y_diff)
|
2024-12-16 22:49:28 +03:00
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
nodes = []
|
|
|
|
if out_of_bounds(one, field):
|
|
|
|
one = None
|
|
|
|
if out_of_bounds(two, field):
|
|
|
|
two = None
|
|
|
|
|
|
|
|
return one, two
|
2024-12-16 22:49:28 +03:00
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
|
2024-12-16 22:49:28 +03:00
|
|
|
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)
|
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
|
2024-12-16 22:49:28 +03:00
|
|
|
def solve1(field: list):
|
|
|
|
pairs = find_pairs(field)
|
|
|
|
antinodes = set()
|
|
|
|
for p in pairs:
|
2024-12-27 22:08:19 +03:00
|
|
|
one, two = find_antinodes(p, field)
|
|
|
|
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
|
|
|
|
|
2024-12-16 22:49:28 +03:00
|
|
|
return len(antinodes)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-12-27 22:08:19 +03:00
|
|
|
#field = Input('input_test2.txt').lines_as_lists()
|
2024-12-16 22:49:28 +03:00
|
|
|
field = Input('input.txt').lines_as_lists()
|
|
|
|
|
2024-12-27 22:08:19 +03:00
|
|
|
# print(find_pairs(field))
|
|
|
|
|
2024-12-16 22:49:28 +03:00
|
|
|
#part 1
|
|
|
|
print('part 1:', solve1(field))
|
|
|
|
|
|
|
|
#part 2
|
2024-12-27 22:08:19 +03:00
|
|
|
print('part 2:', solve2(field))
|