Solved 2024/25 P1

This commit is contained in:
2024-12-25 12:10:22 +01:00
parent add8230d4b
commit 34e7c2a4db
3 changed files with 288 additions and 17 deletions
+32 -17
View File
@@ -2,7 +2,7 @@
import sys,time,re,copy
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile,dijkstra,toGrid,findInGrid,create_graph_from_grid,get_value_in_direction,addTuples
from fred import list2int,get_re,nprint,lprint,loadFile,dijkstra,toGrid,findInGrid,create_graph_from_grid,get_value_in_direction,addTuples,dprint
start_time = time.time()
input_f = 'input'
@@ -24,42 +24,57 @@ def part1():
path, dist = dijkstra(graph,start,end)
cheatWalls = []
for r,row in enumerate(grid):
for c,pos in enumerate(row):
if pos == '.':
if get_value_in_direction(grid,(r,c),'up') == '#' and get_value_in_direction(grid,(r,c),'down') == '#':
cheatWalls.append(addTuples((r,c),(-1,0)))
cheatWalls.append(addTuples((r,c),(1,0)))
if addTuples((r,c),(-1,0)) not in cheatWalls and r != 1 and r != len(grid)-2 and c != 1 and c != len(row)-2:
cheatWalls.append(addTuples((r,c),(-1,0)))
if addTuples((r,c),(1,0)) not in cheatWalls and r != 1 and r != len(grid)-2 and c != 1 and c != len(row)-2:
cheatWalls.append(addTuples((r,c),(1,0)))
elif get_value_in_direction(grid,(r,c),'left') == '#' and get_value_in_direction(grid,(r,c),'right') == '#':
cheatWalls.append(addTuples((r,c),(0,-1)))
cheatWalls.append(addTuples((r,c),(0,1)))
if addTuples((r,c),(0,-1)) not in cheatWalls and r != 1 and r != len(grid)-2 and c != 1 and c != len(row)-2:
cheatWalls.append(addTuples((r,c),(0,-1)))
if addTuples((r,c),(0,1)) not in cheatWalls and r != 1 and r != len(grid)-2 and c != 1 and c != len(row)-2:
cheatWalls.append(addTuples((r,c),(0,1)))
if pos == '#':
if get_value_in_direction(grid,(r,c),'up') == '.' and get_value_in_direction(grid,(r,c),'down') == '.':
cheatWalls.append((r,c))
cheatWalls.append((r,c))
if (r,c) not in cheatWalls and r != 0 and r != len(grid)-1 and c != 0 and c != len(row)-1:
cheatWalls.append((r,c))
elif get_value_in_direction(grid,(r,c),'left') == '.' and get_value_in_direction(grid,(r,c),'right') == '.':
cheatWalls.append((r,c))
cheatWalls.append((r,c))
if (r,c) not in cheatWalls and r != 0 and r != len(grid)-1 and c != 0 and c != len(row)-1:
cheatWalls.append((r,c))
#print(dist)
#nprint(grid,positions=path)
#nprint(grid,positions=cheatWalls)
results = {}
startDist = dist
#print(cheatWalls)
count = 0
for c in cheatWalls:
#print(c)
newGrid = copy.deepcopy(grid)
newGrid[c[0]][c[1]] = '.'
graph = create_graph_from_grid(newGrid,start,end,'#')
path, dist = dijkstra(graph,start,end)
#nprint(grid,(c[0],c[1]),sign='o',positions=path)
#input()
dist = startDist-dist
if dist not in results:
results[dist] = 0
results[dist] += 1
pprint(results)
if dist != 0:
if dist > 100:
count += 1
#if dist not in results:
# results[dist] = 0
#results[dist] += 1
print(count)
print(results)
return count
start_time = time.time()
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')