Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from dijkstar import Graph, find_path
- #Build a list of entries
- input = 'day12_input.txt'
- forest = []
- file1 = open(input, 'r')
- lines = file1.readlines()
- for i,row in enumerate(lines):
- row = row.strip()
- this_row = []
- for j, char in enumerate(row):
- this_row.append(char)
- forest.append(this_row)
- print(forest)
- def getHeight(input):
- if input == 'S': input = 'a'
- elif input == 'E': input = 'z'
- return ord(input)
- def getName(input,i,j):
- return input +str(i)+str(j)
- graph = Graph()
- for i,row in enumerate(forest):
- for j,char in enumerate(row):
- this_val = forest[i][j]
- if this_val == 'S': start_node = getName(this_val,i,j)
- if this_val == 'E': end_node = getName(this_val, i,j)
- if i >0:
- next_val = forest[i-1][j]
- if getHeight(next_val) - getHeight(this_val)<=1:
- graph.add_edge(getName(this_val,i,j), getName(next_val, i-1, j),1)
- if i < len(forest)-1:
- next_val = forest[i+1][j]
- if getHeight(next_val) - getHeight(this_val)<=1:
- graph.add_edge(getName(this_val,i,j), getName(next_val, i+1,j),1)
- if j >0:
- next_val = forest[i][j-1]
- if getHeight(next_val) - getHeight(this_val) <=1:
- graph.add_edge(getName(this_val,i,j), getName(next_val, i, j-1), 1)
- if j < len(row)-1:
- next_val = forest[i][j+1]
- if getHeight(next_val)- getHeight(this_val)<=1:
- graph.add_edge(getName(this_val, i, j), getName(next_val, i, j+1), 1)
- path_info_res = find_path(graph, start_node, end_node)
- print(path_info_res.total_cost)
Advertisement
Add Comment
Please, Sign In to add comment