Spaces:
Running
Running
def solve(): | |
with open("./input.txt", "r") as f: | |
grid = [list(line.strip()) for line in f] | |
rows = len(grid) | |
cols = len(grid[0]) | |
def part1(): | |
antinodes = set() | |
for r1 in range(rows): | |
for c1 in range(cols): | |
freq = grid[r1][c1] | |
for r2 in range(rows): | |
for c2 in range(cols): | |
if grid[r2][c2] == freq and (r1, c1) != (r2, c2): | |
dr = r2 - r1 | |
dc = c2 - c1 | |
r3 = r2 + dr | |
c3 = c2 + dc | |
if 0 <= r3 < rows and 0 <= c3 < cols: | |
antinodes.add((r3, c3)) | |
r3 = r1 - dr | |
c3 = c1 - dc | |
if 0 <= r3 < rows and 0 <= c3 < cols: | |
antinodes.add((r3, c3)) | |
return len(antinodes) | |
def part2(): | |
antinodes = set() | |
for r1 in range(rows): | |
for c1 in range(cols): | |
freq = grid[r1][c1] | |
for r2 in range(rows): | |
for c2 in range(cols): | |
if grid[r2][c2] == freq and (r1, c1) != (r2, c2): | |
for r3 in range(rows): | |
for c3 in range(cols): | |
if (r3*(c2-c1) - c3*(r2-r1)) == (r1*(c2-c1) - c1*(r2-r1)): | |
antinodes.add((r3,c3)) | |
return len(antinodes) | |
print(part1()) | |
print(part2()) | |
solve() |