#!/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,dprint 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') == '#': 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') == '#': 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') == '.': 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') == '.': 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=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 != 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') ######################################### # # # Part 2 # # # ######################################### def part2(): return start_time = time.time() print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')