Skip to main content
Sign in
Snippets Groups Projects
Commit 965da887 authored by BoB's avatar BoB
Browse files

day8

parent 65f1a381
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
```
#08
def readdata(filename):
with open(filename, 'r') as file:
A = [list(line.strip()) for line in file]
return A, len(A)
A, n = readdata('input08.txt')
antenna = {}
for r in range(n):
for c in range(n):
if A[r][c] != ".":
antenna.setdefault(A[r][c], []).append((r, c))
```
%% Cell type:code id: tags:
```
# part 1
B = [[0] * n for _ in range(n)] # position of antinodes
for pos in antenna.values():
for r1, c1 in pos:
for r2, c2 in pos:
if (r1, c1) == (r2, c2):
continue
r_new, c_new = 2*r2-r1, 2*c2-c1 # Calculate antinode position
if 0 <= r_new < n and 0 <= c_new < n:
B[r_new][c_new] = 1
sum(sum(row) for row in B)
```
%% Cell type:code id: tags:
```
# part 2
B = [[0] * n for _ in range(n)] # position of antinodes
for pos in antenna.values():
for r, c in pos:
B[r][c] = 1
for diff_r, diff_c in [(r2 - r, c2 - c) for r2, c2 in pos if (r2, c2) != (r, c)]:
r_new, c_new = r + diff_r, c + diff_c
while 0 <= r_new < n and 0 <= c_new < n:
B[r_new][c_new] = 1
r_new += diff_r
c_new += diff_c
sum(sum(row) for row in B)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment