AdventOfCode/2024/20/solution.py

77 lines
2.7 KiB
Python

#!/bin/python3
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
start_time = time.time()
input_f = 'input'
#########################################
# #
# Part 1 #
# #
#########################################
def part1():
grid = toGrid(input_f)
start = findInGrid(grid,'S')
end = findInGrid(grid,'E')
graph = create_graph_from_grid(grid,start,end,'#')
#nprint(grid)
grid[start[0]][start[1]] = '.'
grid[end[0]][end[1]] = '.'
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)))
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 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))
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))
#print(dist)
#nprint(grid,positions=path)
results = {}
startDist = dist
#print(cheatWalls)
for c in cheatWalls:
newGrid = copy.deepcopy(grid)
newGrid[c[0]][c[1]] = '.'
graph = create_graph_from_grid(newGrid,start,end,'#')
path, dist = dijkstra(graph,start,end)
dist = startDist-dist
if dist not in results:
results[dist] = 0
results[dist] += 1
pprint(results)
start_time = time.time()
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
# #
# Part 2 #
# #
#########################################
def part2():
return
start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')