Add solution for 2021 day 12
This commit is contained in:
parent
d3ef0aeacc
commit
e8ca666e14
@ -15,7 +15,7 @@
|
||||
| 09 | ✓ | ✓ | [C] |
|
||||
| 10 | ✓ | ✓ | [OCaml] |
|
||||
| 11 | ✓ | ✓ | [Crystal] |
|
||||
| 12 | | | |
|
||||
| 12 | ✓ | ✓ | [Python] |
|
||||
| 13 | | | |
|
||||
| 14 | | | |
|
||||
| 15 | | | |
|
||||
@ -47,3 +47,4 @@
|
||||
[c]: http://www.open-std.org/jtc1/sc22/wg14
|
||||
[ocaml]: https://ocaml.org
|
||||
[crystal]: https://crystal-lang.org
|
||||
[python]: https://www.python.org
|
||||
|
2
2021/day-12/.gitignore
vendored
Normal file
2
2021/day-12/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.pyc
|
||||
*.pyo
|
5
2021/day-12/Justfile
Normal file
5
2021/day-12/Justfile
Normal file
@ -0,0 +1,5 @@
|
||||
@part PART INPUT_FILE="inputs/puzzle.txt":
|
||||
python3 part_{{PART}}.py {{INPUT_FILE}}
|
||||
|
||||
clean:
|
||||
rm -rf __pycache__
|
21
2021/day-12/common.py
Normal file
21
2021/day-12/common.py
Normal file
@ -0,0 +1,21 @@
|
||||
def cave_is_small(cave: str) -> bool:
|
||||
return cave.islower()
|
||||
|
||||
|
||||
def add_connection(cave_system, a: str, b: str):
|
||||
if a not in cave_system.keys():
|
||||
cave_system[a] = set([b])
|
||||
else:
|
||||
cave_system[a].add(b)
|
||||
|
||||
|
||||
def parse_file(path: str):
|
||||
cave_system = {}
|
||||
|
||||
with open(path, "r") as f:
|
||||
for line in f.readlines():
|
||||
cave_a, cave_b = line.strip().split("-")
|
||||
add_connection(cave_system, cave_a, cave_b)
|
||||
add_connection(cave_system, cave_b, cave_a)
|
||||
|
||||
return cave_system
|
22
2021/day-12/inputs/puzzle.txt
Normal file
22
2021/day-12/inputs/puzzle.txt
Normal file
@ -0,0 +1,22 @@
|
||||
xx-xh
|
||||
vx-qc
|
||||
cu-wf
|
||||
ny-LO
|
||||
cu-DR
|
||||
start-xx
|
||||
LO-vx
|
||||
cu-LO
|
||||
xx-cu
|
||||
cu-ny
|
||||
xh-start
|
||||
qc-DR
|
||||
vx-AP
|
||||
end-LO
|
||||
ny-DR
|
||||
vx-end
|
||||
DR-xx
|
||||
start-DR
|
||||
end-ny
|
||||
ny-xx
|
||||
xh-DR
|
||||
cu-xh
|
7
2021/day-12/inputs/sample1.txt
Normal file
7
2021/day-12/inputs/sample1.txt
Normal file
@ -0,0 +1,7 @@
|
||||
start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end
|
10
2021/day-12/inputs/sample2.txt
Normal file
10
2021/day-12/inputs/sample2.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
2021/day-12/inputs/sample3.txt
Normal file
18
2021/day-12/inputs/sample3.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
|
47
2021/day-12/part_one.py
Executable file
47
2021/day-12/part_one.py
Executable file
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from common import parse_file, cave_is_small
|
||||
|
||||
|
||||
def search_paths(cave_system, cave="start", marked = set(), paths = [], path = []):
|
||||
path = [*path, cave]
|
||||
|
||||
if cave == "end":
|
||||
return [*paths, path]
|
||||
|
||||
if cave_is_small(cave):
|
||||
marked.add(cave)
|
||||
|
||||
for connected_cave in cave_system[cave]:
|
||||
if connected_cave in marked:
|
||||
continue
|
||||
|
||||
paths = search_paths(cave_system, connected_cave, set(marked), paths, path)
|
||||
|
||||
return paths
|
||||
|
||||
|
||||
def short_path_filter(path):
|
||||
small_caves = []
|
||||
|
||||
for cave in path[1:-1]:
|
||||
if not cave_is_small(cave):
|
||||
if cave in small_caves:
|
||||
return False
|
||||
small_caves.append(cave)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def only_short_paths(paths):
|
||||
return list(filter(short_path_filter, paths))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
cave_system = parse_file(sys.argv[1])
|
||||
|
||||
paths = search_paths(cave_system)
|
||||
|
||||
print(len(only_short_paths(paths)))
|
37
2021/day-12/part_two.py
Normal file
37
2021/day-12/part_two.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from common import parse_file, cave_is_small
|
||||
|
||||
|
||||
def search_paths(cave_system, cave="start", marked = set(), paths = [], path = [], revisit = True):
|
||||
path = [*path, cave]
|
||||
|
||||
if cave == "end":
|
||||
return [*paths, path]
|
||||
|
||||
if cave_is_small(cave):
|
||||
if cave in marked and revisit:
|
||||
revisit = False
|
||||
|
||||
marked.add(cave)
|
||||
|
||||
for connected_cave in cave_system[cave]:
|
||||
if connected_cave == "start":
|
||||
continue
|
||||
|
||||
if connected_cave in marked and not revisit:
|
||||
continue
|
||||
|
||||
paths = search_paths(cave_system, connected_cave, set(marked), paths, path, revisit)
|
||||
|
||||
return paths
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
cave_system = parse_file(sys.argv[1])
|
||||
|
||||
paths = search_paths(cave_system)
|
||||
|
||||
print(len(paths))
|
Loading…
Reference in New Issue
Block a user