This commit is contained in:
Stefan Harmuth 2021-12-12 07:01:26 +01:00
parent 67479e5045
commit 457be373ff
10 changed files with 190 additions and 0 deletions

54
day12.py Normal file
View File

@ -0,0 +1,54 @@
from tools.aoc import AOCDay
from typing import Any, Dict, List
def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]:
m = {}
for l in lines:
if l[0] in m:
m[l[0]].append(l[1])
else:
m[l[0]] = [l[1]]
if l[1] in m:
m[l[1]].append(l[0])
else:
m[l[1]] = [l[0]]
return m
def getPaths(caveMap: Dict[str, List[str]], start: str, visited: List[str], noDouble: bool = True) -> List[List[str]]:
paths = []
visited.append(start)
for thisCave in caveMap[start]:
if thisCave == 'start':
continue
if thisCave == 'end':
paths.append(['end'])
continue
foundDouble = False
if thisCave.islower() and thisCave in visited:
if noDouble:
continue
foundDouble = True
sub_paths = getPaths(caveMap, thisCave, visited.copy(), noDouble or foundDouble)
[paths.append(x) for x in sub_paths if 'end' in x]
return paths
class Day(AOCDay):
test_solutions_p1 = [10, 19, 226, 5076]
test_solutions_p2 = [36, 103, 3509, 145643]
def part1(self) -> Any:
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
return len(getPaths(caveMap, 'start', []))
def part2(self) -> Any:
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
return len(getPaths(caveMap, 'start', [], False))

22
inputs/input12 Normal file
View File

@ -0,0 +1,22 @@
end-MY
MY-xc
ho-NF
start-ho
NF-xc
NF-yf
end-yf
xc-TP
MY-qo
yf-TP
dc-NF
dc-xc
start-dc
yf-MY
MY-ho
EM-uh
xc-yf
ho-dc
uh-NF
yf-ho
end-uh
start-NF

7
inputs/test_input12_1_0 Normal file
View File

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

10
inputs/test_input12_1_1 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
inputs/test_input12_1_2 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

22
inputs/test_input12_1_3 Normal file
View File

@ -0,0 +1,22 @@
end-MY
MY-xc
ho-NF
start-ho
NF-xc
NF-yf
end-yf
xc-TP
MY-qo
yf-TP
dc-NF
dc-xc
start-dc
yf-MY
MY-ho
EM-uh
xc-yf
ho-dc
uh-NF
yf-ho
end-uh
start-NF

7
inputs/test_input12_2_0 Normal file
View File

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

10
inputs/test_input12_2_1 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
inputs/test_input12_2_2 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

22
inputs/test_input12_2_3 Normal file
View File

@ -0,0 +1,22 @@
end-MY
MY-xc
ho-NF
start-ho
NF-xc
NF-yf
end-yf
xc-TP
MY-qo
yf-TP
dc-NF
dc-xc
start-dc
yf-MY
MY-ho
EM-uh
xc-yf
ho-dc
uh-NF
yf-ho
end-uh
start-NF