This commit is contained in:
FrederikBaerentsen 2024-12-09 12:07:06 +01:00
commit d55b9df928
2 changed files with 54 additions and 35 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
input
.env
test
test*
p2.py
r/*

View File

@ -5,41 +5,35 @@ sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,grid_valid,loadFile,subTuples,addTuples,toGrid,get_value_in_direction
start_time = time.time()
input_f = 'test'
input_f = 'input'
part = 1
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
def findAntinodes(grid:list,pos:set,current_node:set):
print('Found',current_node,'at',pos)
antinodes = []
for row in range(pos[0],len(grid)):
for col in range(0,len(grid[1])):
print(row,col)
if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos:
print('Matched with',(row,col))
s = subTuples((row,col-1),pos)
a = addTuples(s,(row,col+1))
if grid_valid(s[0],s[1],grid):
print('Antinode at',s)
antinodes.append(s)
if grid_valid(a[0],a[1],grid):
print('Antinode at',a)
antinodes.append(a)
dist = subTuples((row,col),pos)
add = addTuples(dist,(row,col))
sub = subTuples(pos,dist)
if grid_valid(sub[0],sub[1],grid):
antinodes.append(sub)
if grid_valid(add[0],add[1],grid):
antinodes.append(add)
return antinodes
if part == 1:
grid = toGrid(input_f)
nprint(grid)
size = (len(grid),len(grid[0]))
antinodes = []
for row in range(0,size[0]):
@ -47,14 +41,7 @@ if part == 1:
current_node = get_value_in_direction(grid,(row,col))
if current_node != '.':
antinodes += findAntinodes(grid,(row,col),current_node)
print(antinodes)
for idx, i in enumerate(grid):
for jdx, j in enumerate(i):
if (idx, jdx) in antinodes:
print('#', end=' ') # Print sign
else:
print(grid[idx][jdx], end=' ') # Regular grid element
print()
print(len(set(antinodes)))
#########################################
@ -63,6 +50,37 @@ if part == 1:
# #
#########################################
if part == 2:
exit()
def findAntinodes(grid:list,pos:set,current_node:set):
antinodes = []
for row in range(0,len(grid)):
for col in range(0,len(grid[1])):
if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos:
dist = subTuples((row,col),pos)
add = pos
sub = pos
while grid_valid(add[0],add[1],grid):
add = addTuples(dist,add)
if grid_valid(add[0],add[1],grid):
antinodes.append(add)
while grid_valid(sub[0],sub[1],grid):
sub = subTuples(sub,dist)
if grid_valid(sub[0],sub[1],grid):
antinodes.append(sub)
return antinodes
grid = toGrid(input_f)
size = (len(grid),len(grid[0]))
antinodes = []
for row in range(0,size[0]):
for col in range(0,size[1]):
current_node = get_value_in_direction(grid,(row,col))
if current_node != '.':
antinodes += findAntinodes(grid,(row,col),current_node)
print(len(set(antinodes)))
print("--- %s seconds ---" % (time.time() - start_time))