This commit is contained in:
		
							parent
							
								
									b3234118da
								
							
						
					
					
						commit
						26313578e3
					
				
					 2 changed files with 94 additions and 1 deletions
				
			
		
							
								
								
									
										2
									
								
								12/12.py
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								12/12.py
									
									
									
									
									
								
							| 
						 | 
					@ -61,7 +61,7 @@ all_path = []
 | 
				
			||||||
explore_tree('start', visited_nodes, all_path, 0)
 | 
					explore_tree('start', visited_nodes, all_path, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print()
 | 
					print()
 | 
				
			||||||
print(f'listing all paths')
 | 
					print('listing all paths')
 | 
				
			||||||
for p in all_path:
 | 
					for p in all_path:
 | 
				
			||||||
    print(p)
 | 
					    print(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										93
									
								
								12/12_2.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										93
									
								
								12/12_2.py
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,93 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					# 2021 - Advent Of Code - 11
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					GRID_SIZE = 10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse_file(file):
 | 
				
			||||||
 | 
					    with open(file) as f:
 | 
				
			||||||
 | 
					        return [x.strip() for x in f.readlines()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def special_cave(point, twice_cave):
 | 
				
			||||||
 | 
					    if point == twice_cave[0] and twice_cave[1] > 0:
 | 
				
			||||||
 | 
					        print(f'Using special cave: {twice_cave}')
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					    return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def valid_destination(point, visited, twice_cave):
 | 
				
			||||||
 | 
					    if point.isupper():
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					    if point not in visited:
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					    if point == twice_cave and visited.count(point) < 2:
 | 
				
			||||||
 | 
					        print(f'Using special cave: {twice_cave}')
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					    return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def explore_tree(current_node, visited, paths_list, twice_cave, gen):
 | 
				
			||||||
 | 
					    possible_path = [c for c in connections if current_node in c.split('-')]
 | 
				
			||||||
 | 
					    print(f'\n{gen} current_node: {current_node}\t possible path: {possible_path}')
 | 
				
			||||||
 | 
					    print(f'{gen} visited: {visited}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for path in possible_path:
 | 
				
			||||||
 | 
					        print(f'{gen} exploring {path}')
 | 
				
			||||||
 | 
					        new_visited = list(visited)
 | 
				
			||||||
 | 
					        # check end
 | 
				
			||||||
 | 
					        if 'end' in path:
 | 
				
			||||||
 | 
					            visited.append('end')
 | 
				
			||||||
 | 
					            paths_list.add(tuple(visited))
 | 
				
			||||||
 | 
					            print(f'{gen} keeping path: {visited}')
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        nodes = path.split('-')
 | 
				
			||||||
 | 
					        if nodes[0] == current_node:
 | 
				
			||||||
 | 
					            if valid_destination(nodes[1], visited, twice_cave):
 | 
				
			||||||
 | 
					                print(f'{gen} destination: {nodes[1]}')
 | 
				
			||||||
 | 
					                new_visited.append(nodes[1])
 | 
				
			||||||
 | 
					                explore_tree(nodes[1], list(new_visited), paths_list, twice_cave, gen+1)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                print(f'{gen} removing dest: {nodes[1]}')
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            if valid_destination(nodes[0], visited, twice_cave):
 | 
				
			||||||
 | 
					                print(f'{gen} destination: {nodes[0]}')
 | 
				
			||||||
 | 
					                new_visited.append(nodes[0])
 | 
				
			||||||
 | 
					                explore_tree(nodes[0], list(new_visited), paths_list, twice_cave, gen+1)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                print(f'{gen} removing dest: {nodes[0]}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# connections = parse_file('input_example3.txt')
 | 
				
			||||||
 | 
					connections = parse_file('input.txt')
 | 
				
			||||||
 | 
					print(f'connections: {connections}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					points = set()
 | 
				
			||||||
 | 
					for conn in connections:
 | 
				
			||||||
 | 
					    p = conn.split('-')
 | 
				
			||||||
 | 
					    points.add(p[0])
 | 
				
			||||||
 | 
					    points.add(p[1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f'unique points: {points}')
 | 
				
			||||||
 | 
					print()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					starting_positions = [c for c in connections if 'start' in c]
 | 
				
			||||||
 | 
					ending_positions = [c for c in connections if 'end' in c]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					small_caves = [c for c in points if c != 'start' and c != 'end' and c.islower()]
 | 
				
			||||||
 | 
					print(f'small caves: {small_caves}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					visited_nodes = ['start']
 | 
				
			||||||
 | 
					all_path = set()
 | 
				
			||||||
 | 
					for cave in small_caves:
 | 
				
			||||||
 | 
					    explore_tree('start', visited_nodes, all_path, cave, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					explore_tree('start', visited_nodes, all_path, 'b', 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print()
 | 
				
			||||||
 | 
					print('listing all paths')
 | 
				
			||||||
 | 
					for p in all_path:
 | 
				
			||||||
 | 
					    print(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f'{len(all_path)} paths')
 | 
				
			||||||
		Loading…
	
		Reference in a new issue