76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
#!/bin/python3
|
|
import sys,time,re
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import list2int,get_re,nprint,lprint,loadFile,bfs,get_value_in_direction,addTuples,grid_valid
|
|
start_time = time.time()
|
|
|
|
input_f = 'input'
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
def part1():
|
|
instructions = []
|
|
grid = []
|
|
w = 70
|
|
h = 70
|
|
end = (h,w)
|
|
start = (0,0)
|
|
with open(input_f) as file:
|
|
for line in file:
|
|
l = list2int(line.rstrip().split(','))
|
|
instructions.append((l[0],l[1]))
|
|
#print(instructions)
|
|
|
|
grid = [[ '.' for x in range(0,w+1)] for y in range(0,h+1)]
|
|
|
|
for i in range(1024):
|
|
x = instructions[i]
|
|
grid[x[0]][x[1]] = '#'
|
|
|
|
|
|
|
|
def is_goal(node):
|
|
#print(node)
|
|
|
|
return True if node == end else False
|
|
|
|
def get_neighbors(node):
|
|
directions = ['up','down','left','right']
|
|
offsets = {
|
|
'up': (-1, 0),
|
|
'down': (1, 0),
|
|
'left': (0, -1),
|
|
'right': (0, 1),
|
|
}
|
|
neighbors = []
|
|
|
|
# Loop through all the directions
|
|
for d in directions:
|
|
tmp = addTuples(offsets[d],node)
|
|
if get_value_in_direction(grid,node,d) != '#' and grid_valid(tmp[0],tmp[1],grid):
|
|
neighbors.append((tmp[0],tmp[1]))
|
|
# Return the list of valid neighbors
|
|
return neighbors
|
|
|
|
goal_nodes, path = bfs((0,0),is_goal,get_neighbors)
|
|
print(goal_nodes)
|
|
return len(path[goal_nodes[0]])-1
|
|
|
|
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') |