File size: 1,645 Bytes
a4da721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()