[2021] day 12 python

This commit is contained in:
Maciej Jur 2021-12-12 12:57:30 +01:00
parent 028dc17a3c
commit d4524174cc
2 changed files with 71 additions and 0 deletions

23
2021/.input/day12 Normal file
View file

@ -0,0 +1,23 @@
start-YA
ps-yq
zt-mu
JS-yi
yq-VJ
QT-ps
start-yq
YA-yi
start-nf
nf-YA
nf-JS
JS-ez
yq-JS
ps-JS
ps-yi
yq-nf
QT-yi
end-QT
nf-yi
zt-QT
end-ez
yq-YA
end-JS

48
2021/Python/day12.py Normal file
View file

@ -0,0 +1,48 @@
import re
input_pattern = re.compile(r'([a-zA-Z]+)-([a-zA-Z]+)')
small_pattern = re.compile(r'([a-z]+)')
def load():
with open('../.input/day12', 'r') as f:
return input_pattern.findall(f.read())
def construct_paths(graph, start, end, path=None, single_reentry=False):
if path is None:
path = [start]
if start == end:
yield path
else:
for node in graph[start]:
is_small = small_pattern.match(node)
if not is_small or node not in path:
yield from construct_paths(graph, node, end, path + [node], single_reentry)
elif is_small and single_reentry and node not in ["start", "end"]:
yield from construct_paths(graph, node, end, path + [node])
def solve1():
graph = {}
for node1, node2 in load():
graph.setdefault(node1, set()).add(node2)
graph.setdefault(node2, set()).add(node1)
return sum(1 for _ in construct_paths(graph, 'start', 'end'))
def solve2():
graph = {}
for node1, node2 in load():
graph.setdefault(node1, set()).add(node2)
graph.setdefault(node2, set()).add(node1)
return sum(1 for _ in construct_paths(graph, 'start', 'end', single_reentry=True))
if __name__ == '__main__':
print(solve1()) # 4304
print(solve2()) # 118242