This commit is contained in:
parent
014a263cd1
commit
b3234118da
5 changed files with 129 additions and 0 deletions
68
12/12.py
Executable file
68
12/12.py
Executable file
|
@ -0,0 +1,68 @@
|
||||||
|
#!/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 explore_tree(current_node, visited, paths_list, 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.append(visited)
|
||||||
|
print(f'{gen} keeping path: {visited}')
|
||||||
|
continue
|
||||||
|
|
||||||
|
nodes = path.split('-')
|
||||||
|
if nodes[0] == current_node:
|
||||||
|
if nodes[1] not in visited or nodes[1].isupper():
|
||||||
|
print(f'{gen} destination: {nodes[1]}')
|
||||||
|
new_visited.append(nodes[1])
|
||||||
|
explore_tree(nodes[1], list(new_visited), paths_list, gen+1)
|
||||||
|
else:
|
||||||
|
print(f'{gen} removing dest: {nodes[1]}')
|
||||||
|
else:
|
||||||
|
if nodes[0] not in visited or nodes[0].isupper():
|
||||||
|
print(f'{gen} destination: {nodes[0]}')
|
||||||
|
new_visited.append(nodes[0])
|
||||||
|
explore_tree(nodes[0], list(new_visited), paths_list, gen+1)
|
||||||
|
else:
|
||||||
|
print(f'{gen} removing dest: {nodes[0]}')
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
visited_nodes = ['start']
|
||||||
|
all_path = []
|
||||||
|
explore_tree('start', visited_nodes, all_path, 0)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(f'listing all paths')
|
||||||
|
for p in all_path:
|
||||||
|
print(p)
|
||||||
|
|
||||||
|
print(f'{len(all_path)} paths')
|
26
12/input.txt
Normal file
26
12/input.txt
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
end-ry
|
||||||
|
jf-jb
|
||||||
|
jf-IO
|
||||||
|
jb-hz
|
||||||
|
jo-LM
|
||||||
|
hw-end
|
||||||
|
hw-LM
|
||||||
|
hz-ry
|
||||||
|
WI-start
|
||||||
|
LM-start
|
||||||
|
kd-jf
|
||||||
|
xi-WI
|
||||||
|
hw-jb
|
||||||
|
hz-jf
|
||||||
|
LM-jb
|
||||||
|
jb-xi
|
||||||
|
ry-jf
|
||||||
|
WI-jb
|
||||||
|
end-hz
|
||||||
|
jo-start
|
||||||
|
WI-jo
|
||||||
|
xi-ry
|
||||||
|
xi-LM
|
||||||
|
xi-hw
|
||||||
|
jo-xi
|
||||||
|
WI-jf
|
7
12/input_example1.txt
Normal file
7
12/input_example1.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
start-A
|
||||||
|
start-b
|
||||||
|
A-c
|
||||||
|
A-b
|
||||||
|
b-d
|
||||||
|
A-end
|
||||||
|
b-end
|
10
12/input_example2.txt
Normal file
10
12/input_example2.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
dc-end
|
||||||
|
HN-start
|
||||||
|
start-kj
|
||||||
|
dc-start
|
||||||
|
dc-HN
|
||||||
|
LN-dc
|
||||||
|
HN-end
|
||||||
|
kj-sa
|
||||||
|
kj-HN
|
||||||
|
kj-dc
|
18
12/input_example3.txt
Normal file
18
12/input_example3.txt
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
fs-end
|
||||||
|
he-DX
|
||||||
|
fs-he
|
||||||
|
start-DX
|
||||||
|
pj-DX
|
||||||
|
end-zg
|
||||||
|
zg-sl
|
||||||
|
zg-pj
|
||||||
|
pj-he
|
||||||
|
RW-he
|
||||||
|
fs-DX
|
||||||
|
pj-RW
|
||||||
|
zg-RW
|
||||||
|
start-pj
|
||||||
|
he-WI
|
||||||
|
zg-he
|
||||||
|
pj-fs
|
||||||
|
start-RW
|
Loading…
Reference in a new issue