Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class node():
- def __init__(self,name):
- self.name = name
- self.adj_nodes = {}
- def add_node(self,name,dir):
- self.adj_nodes[dir] = name
- def get_adj_node(self,dir):
- return self.adj_nodes[dir]
- def parttwo(nodes,dir):
- #first find all nodes that end if A or Z
- current_nodes = [x for x in nodes if x.name[-1] == "A"]
- end_nodes = [x.name for x in nodes if x.name[-1] == "Z"]
- total_steps = 0
- count = 0
- keep_mapping = True
- while keep_mapping:
- if total_steps % 1000000 == 0:
- print(total_steps)
- if count >= len(dir):
- count = 0
- step = dir[count]
- next_list = []
- is_it_over = True
- for n in current_nodes:
- next_list.append(n.get_adj_node(step))
- if next_list[-1] not in end_nodes:
- is_it_over = False
- count +=1
- total_steps +=1
- if is_it_over:
- keep_mapping = False
- else:
- current_nodes = []
- for n in next_list:
- current_nodes.append([x for x in nodes if x.name == n][0])
- print(f"Steps to end_nodes is:", total_steps)
Advertisement
Add Comment
Please, Sign In to add comment