2024-12-24 09:30:21 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,time,re,copy
|
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
2024-12-25 12:10:22 +01:00
|
|
|
from fred import list2int,get_re,nprint,lprint,loadFile,dijkstra,toGrid,findInGrid,create_graph_from_grid,get_value_in_direction,addTuples,dprint
|
2024-12-24 09:30:21 +01:00
|
|
|
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 = []
|
2024-12-25 12:10:22 +01:00
|
|
|
|
2024-12-24 09:30:21 +01:00
|
|
|
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') == '#':
|
2024-12-25 12:10:22 +01:00
|
|
|
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)))
|
2024-12-24 09:30:21 +01:00
|
|
|
elif get_value_in_direction(grid,(r,c),'left') == '#' and get_value_in_direction(grid,(r,c),'right') == '#':
|
2024-12-25 12:10:22 +01:00
|
|
|
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)))
|
|
|
|
|
2024-12-24 09:30:21 +01:00
|
|
|
if pos == '#':
|
|
|
|
if get_value_in_direction(grid,(r,c),'up') == '.' and get_value_in_direction(grid,(r,c),'down') == '.':
|
2024-12-25 12:10:22 +01:00
|
|
|
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))
|
2024-12-24 09:30:21 +01:00
|
|
|
elif get_value_in_direction(grid,(r,c),'left') == '.' and get_value_in_direction(grid,(r,c),'right') == '.':
|
2024-12-25 12:10:22 +01:00
|
|
|
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))
|
|
|
|
|
2024-12-24 09:30:21 +01:00
|
|
|
|
|
|
|
#print(dist)
|
2024-12-25 12:10:22 +01:00
|
|
|
#nprint(grid,positions=cheatWalls)
|
2024-12-24 09:30:21 +01:00
|
|
|
|
|
|
|
results = {}
|
|
|
|
startDist = dist
|
|
|
|
#print(cheatWalls)
|
2024-12-25 12:10:22 +01:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
2024-12-24 09:30:21 +01:00
|
|
|
for c in cheatWalls:
|
2024-12-25 12:10:22 +01:00
|
|
|
#print(c)
|
2024-12-24 09:30:21 +01:00
|
|
|
newGrid = copy.deepcopy(grid)
|
|
|
|
newGrid[c[0]][c[1]] = '.'
|
|
|
|
graph = create_graph_from_grid(newGrid,start,end,'#')
|
|
|
|
path, dist = dijkstra(graph,start,end)
|
2024-12-25 12:10:22 +01:00
|
|
|
#nprint(grid,(c[0],c[1]),sign='o',positions=path)
|
|
|
|
#input()
|
2024-12-24 09:30:21 +01:00
|
|
|
dist = startDist-dist
|
2024-12-25 12:10:22 +01:00
|
|
|
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
|
2024-12-24 09:30:21 +01:00
|
|
|
|
|
|
|
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')
|