-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_1.py
42 lines (33 loc) · 1.19 KB
/
day12_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import List, Set
@dataclass
class CavePath:
path: List
small_caves: Set
ended: bool = False
inputs = [line.split('-') for line in Path(sys.argv[1]).read_text().split('\n') if line]
transitions = defaultdict(list)
for line in inputs:
transitions[line[0]].append(line[1])
transitions[line[1]].append(line[0])
paths = [CavePath(path=['start'], small_caves={'start'})]
while not all(p.ended for p in paths):
new_paths = []
for p in paths:
last_point = p.path[-1]
next_points = transitions[last_point]
if last_point == 'end' or not next_points:
new_paths.append(CavePath(path=p.path, ended=True, small_caves=p.small_caves))
continue
for next_point in next_points:
small_caves = p.small_caves
if next_point.islower():
if next_point in p.small_caves:
continue
small_caves = small_caves | {next_point}
new_paths.append(CavePath(path=p.path + [next_point], small_caves=small_caves))
paths = new_paths
print(f'Len: {len(paths)}')