added day 12

This commit is contained in:
Peter Hudec 2021-12-12 12:27:34 +01:00
parent b794684673
commit 87cfdc8ea0
6 changed files with 136 additions and 0 deletions

23
12/input01.txt Normal file
View File

@ -0,0 +1,23 @@
he-JK
wy-KY
pc-XC
vt-wy
LJ-vt
wy-end
wy-JK
end-LJ
start-he
JK-end
pc-wy
LJ-pc
at-pc
xf-XC
XC-he
pc-JK
vt-XC
at-he
pc-he
start-at
start-XC
at-LJ
vt-JK

7
12/input01_sample01.txt Normal file
View File

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

10
12/input01_sample02.txt Normal file
View 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/input01_sample03.txt Normal file
View 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

37
12/solve01.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
import copy
graph = dict()
def calculate_paths(graph, current, small_seen, path):
found = 0
for n in graph.get(current):
if n == 'start':
continue
if n in small_seen:
continue
p = copy.copy(path)
p.append(n)
#print(p)
#print("%s -> %s" % (current, n))
if n == 'end':
found += 1
continue
sm = copy.copy(small_seen)
if n.lower() == n:
sm.append(n)
found += calculate_paths(graph, n, sm, p)
return found
with open("input01.txt","r") as f:
for line in f:
(x,y) = line.strip().split("-",2)
if x not in graph:
graph[x] = []
graph[x].append(y)
if y not in graph:
graph[y] = []
graph[y].append(x)
#print(graph)
print(calculate_paths(graph, 'start', [], ['start']))

41
12/solve02.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python
import copy
from collections import defaultdict
graph = dict()
def calculate_paths(graph, current, small_seen, path):
if current == 'end':
return 1
found = 0
for n in sorted(graph.get(current)):
if n == 'start':
continue
# already seen, check if there is any with count
if small_seen[n] > 0:
tmp = max(small_seen.values())
if tmp > 1:
continue
p = copy.copy(path)
p.append(n)
#print("%s -> %s" % (current, n))
sm = copy.copy(small_seen)
if n.lower() == n:
sm[n] += 1
found += calculate_paths(graph, n, sm, p)
return found
with open("input01.txt","r") as f:
for line in f:
(x,y) = line.strip().split("-",2)
if x not in graph:
graph[x] = []
graph[x].append(y)
if y not in graph:
graph[y] = []
graph[y].append(x)
#print(graph)
small_seen = defaultdict(int)
print(calculate_paths(graph, 'start', small_seen, ['start']))