Solved 2024/08 P2

This commit is contained in:
FrederikBaerentsen 2024-12-08 19:37:18 +01:00
parent 3f10118667
commit ee293b7a6f

View File

@ -5,7 +5,7 @@ sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,grid_valid,loadFile,subTuples,addTuples,toGrid,get_value_in_direction from fred import list2int,get_re,nprint,lprint,grid_valid,loadFile,subTuples,addTuples,toGrid,get_value_in_direction
start_time = time.time() start_time = time.time()
input_f = 'test3' input_f = 'input'
part = 2 part = 2
######################################### #########################################
@ -16,30 +16,24 @@ part = 2
if part == 1: if part == 1:
def findAntinodes(grid:list,pos:set,current_node:set): def findAntinodes(grid:list,pos:set,current_node:set):
#print('Found',current_node,'at',pos)
antinodes = [] antinodes = []
for row in range(pos[0],len(grid)): for row in range(pos[0],len(grid)):
for col in range(0,len(grid[1])): for col in range(0,len(grid[1])):
if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos:
#print('Matched with',(row,col))
dist = subTuples((row,col),pos) dist = subTuples((row,col),pos)
add = addTuples(dist,(row,col)) add = addTuples(dist,(row,col))
sub = subTuples(pos,dist) sub = subTuples(pos,dist)
if grid_valid(sub[0],sub[1],grid): if grid_valid(sub[0],sub[1],grid):
#print('Antinode at',sub)
antinodes.append(sub) antinodes.append(sub)
if grid_valid(add[0],add[1],grid): if grid_valid(add[0],add[1],grid):
#print('Antinode at',add)
antinodes.append(add) antinodes.append(add)
return antinodes return antinodes
grid = toGrid(input_f) grid = toGrid(input_f)
nprint(grid)
size = (len(grid),len(grid[0])) size = (len(grid),len(grid[0]))
antinodes = [] antinodes = []
for row in range(0,size[0]): for row in range(0,size[0]):
@ -47,15 +41,6 @@ if part == 1:
current_node = get_value_in_direction(grid,(row,col)) current_node = get_value_in_direction(grid,(row,col))
if current_node != '.': if current_node != '.':
antinodes += findAntinodes(grid,(row,col),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))) print(len(set(antinodes)))
@ -66,30 +51,29 @@ if part == 1:
######################################### #########################################
if part == 2: if part == 2:
def findAntinodes(grid:list,pos:set,current_node:set): def findAntinodes(grid:list,pos:set,current_node:set):
#print('Found',current_node,'at',pos)
antinodes = [] antinodes = []
for row in range(pos[0],len(grid)): for row in range(0,len(grid)):
for col in range(0,len(grid[1])): for col in range(0,len(grid[1])):
if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos:
#print('Matched with',(row,col))
dist = subTuples((row,col),pos) dist = subTuples((row,col),pos)
add = addTuples(dist,(row,col)) add = pos
sub = subTuples(pos,dist) sub = pos
if grid_valid(sub[0],sub[1],grid): while grid_valid(add[0],add[1],grid):
#print('Antinode at',sub) add = addTuples(dist,add)
antinodes.append(sub) if grid_valid(add[0],add[1],grid):
if grid_valid(add[0],add[1],grid): antinodes.append(add)
#print('Antinode at',add)
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 return antinodes
grid = toGrid(input_f) grid = toGrid(input_f)
nprint(grid)
size = (len(grid),len(grid[0])) size = (len(grid),len(grid[0]))
antinodes = [] antinodes = []
for row in range(0,size[0]): for row in range(0,size[0]):
@ -97,15 +81,6 @@ if part == 2:
current_node = get_value_in_direction(grid,(row,col)) current_node = get_value_in_direction(grid,(row,col))
if current_node != '.': if current_node != '.':
antinodes += findAntinodes(grid,(row,col),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))) print(len(set(antinodes)))
print("--- %s seconds ---" % (time.time() - start_time)) print("--- %s seconds ---" % (time.time() - start_time))