From 88f2b091746b986cdf9c4b5e1f69a28ec21c72a4 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 8 Dec 2024 19:24:10 +0100 Subject: [PATCH 1/3] Solved 2024/08 P1 --- 2024/08/solution.py | 95 ++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 26 deletions(-) diff --git a/2024/08/solution.py b/2024/08/solution.py index 98f93b6..454069b 100644 --- a/2024/08/solution.py +++ b/2024/08/solution.py @@ -5,37 +5,37 @@ 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 = 'test3' -part = 1 +part = 2 ######################################### # # # 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) - - return antinodes - 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])): + + if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: + #print('Matched with',(row,col)) + + dist = subTuples((row,col),pos) + add = addTuples(dist,(row,col)) + sub = subTuples(pos,dist) + if grid_valid(sub[0],sub[1],grid): + #print('Antinode at',sub) + antinodes.append(sub) + if grid_valid(add[0],add[1],grid): + #print('Antinode at',add) + antinodes.append(add) + + return antinodes + grid = toGrid(input_f) nprint(grid) @@ -47,7 +47,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) + #print(antinodes) for idx, i in enumerate(grid): for jdx, j in enumerate(i): if (idx, jdx) in antinodes: @@ -56,6 +56,8 @@ if part == 1: print(grid[idx][jdx], end=' ') # Regular grid element print() + print(len(set(antinodes))) + ######################################### # # @@ -63,6 +65,47 @@ if part == 1: # # ######################################### if part == 2: - exit() + 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("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file + if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: + #print('Matched with',(row,col)) + + dist = subTuples((row,col),pos) + add = addTuples(dist,(row,col)) + sub = subTuples(pos,dist) + if grid_valid(sub[0],sub[1],grid): + #print('Antinode at',sub) + antinodes.append(sub) + if grid_valid(add[0],add[1],grid): + #print('Antinode at',add) + antinodes.append(add) + + return antinodes + + grid = toGrid(input_f) + + nprint(grid) + + 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(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("--- %s seconds ---" % (time.time() - start_time)) From 3f10118667d2e2739bbe7327a2001e6bdf702ce5 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 8 Dec 2024 19:24:52 +0100 Subject: [PATCH 2/3] Updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 17b32dc..15f8ee2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ input .env test +test* p2.py r/* From ee293b7a6f4a0f527340e6eadbb314e176836133 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 8 Dec 2024 19:37:18 +0100 Subject: [PATCH 3/3] Solved 2024/08 P2 --- 2024/08/solution.py | 51 ++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 38 deletions(-) diff --git a/2024/08/solution.py b/2024/08/solution.py index 454069b..a7d2c57 100644 --- a/2024/08/solution.py +++ b/2024/08/solution.py @@ -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 start_time = time.time() -input_f = 'test3' +input_f = 'input' part = 2 ######################################### @@ -16,30 +16,24 @@ part = 2 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])): if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: - #print('Matched with',(row,col)) dist = subTuples((row,col),pos) add = addTuples(dist,(row,col)) sub = subTuples(pos,dist) if grid_valid(sub[0],sub[1],grid): - #print('Antinode at',sub) antinodes.append(sub) if grid_valid(add[0],add[1],grid): - #print('Antinode at',add) antinodes.append(add) return antinodes grid = toGrid(input_f) - nprint(grid) - size = (len(grid),len(grid[0])) antinodes = [] for row in range(0,size[0]): @@ -47,15 +41,6 @@ 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))) @@ -66,30 +51,29 @@ if part == 1: ######################################### if part == 2: 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 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: - #print('Matched with',(row,col)) dist = subTuples((row,col),pos) - add = addTuples(dist,(row,col)) - sub = subTuples(pos,dist) - if grid_valid(sub[0],sub[1],grid): - #print('Antinode at',sub) - antinodes.append(sub) - if grid_valid(add[0],add[1],grid): - #print('Antinode at',add) - antinodes.append(add) + 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) - nprint(grid) - size = (len(grid),len(grid[0])) antinodes = [] for row in range(0,size[0]): @@ -97,15 +81,6 @@ if part == 2: 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))) print("--- %s seconds ---" % (time.time() - start_time))