1
- from functools import total_ordering
2
1
import re
3
2
4
- @total_ordering
5
3
class Valve :
6
4
def __init__ (self , name , flow , to_names ):
7
5
self .name = name
@@ -12,24 +10,31 @@ def __init__(self, name, flow, to_names):
12
10
self .flowed = 0
13
11
14
12
def __eq__ (self , other ):
15
- return self .name .__eq__ (other .name )
13
+ return self .name == other .name
14
+
15
+ def __ne__ (self , other ):
16
+ return self .name != other .name
16
17
17
18
def __lt__ (self , other ):
18
- return self .flow . __lt__ ( other .flow )
19
+ return self .flow < other .flow
19
20
20
21
def __le__ (self , other ):
21
- return self .flow . __le__ ( other .flow )
22
+ return self .flow <= other .flow
22
23
23
24
def __gt__ (self , other ):
24
- return self .flow . __gt__ ( other .flow )
25
+ return self .flow > other .flow
25
26
26
27
def __ge__ (self , other ):
27
- return self .flow . __ge__ ( other .flow )
28
+ return self .flow >= other .flow
28
29
29
30
def tick (self ):
30
31
if self .open :
31
32
self .flowed += self .flow
32
33
34
+ def reset (self ):
35
+ self .open = False
36
+ self .flowed = 0
37
+
33
38
class Cave :
34
39
def __init__ (self , filename ) -> None :
35
40
with open (filename ) as file :
@@ -49,15 +54,33 @@ def __init__(self, filename) -> None:
49
54
self .current = self .valves [0 ]
50
55
51
56
def do (self , minutes = 30 ):
52
- for _ in range (minutes ):
53
- for valve in self .valves :
54
- valve .tick ()
55
- # One deep search
56
- if most_promising := max (valve for valve in self .current .to if not valve .open ):
57
- if most_promising .flow > self .current .flow + 1 or self .current .open :
58
- print (f'You move to valve { most_promising .name } .' )
59
- self .current = most_promising
60
- continue
61
- print (f'You open valve { self .current .name } .' )
62
- self .current .open = True
63
- return sum ([valve .flowed for valve in self .valves ])
57
+ all_routes = self .routes ()
58
+
59
+
60
+ def routes (self , start = None , prior = [], max_depth = 30 ):
61
+ if not start :
62
+ start = self .current
63
+ if max_depth == 0 :
64
+ return prior
65
+ result = []
66
+ if f'open{ start .name } ' not in prior :
67
+ result .extend (
68
+ self .routes (
69
+ start = start ,
70
+ prior = prior .append (f'open{ start .name } ' ),
71
+ max_depth = max_depth - 1
72
+ )
73
+ )
74
+ for valve in start .to :
75
+ result .extend (
76
+ self .routes (
77
+ start = valve ,
78
+ prior = prior .append (valve .name ),
79
+ max_depth = max_depth - 1
80
+ )
81
+ )
82
+ return result
83
+
84
+ def score_route (self , route ):
85
+
86
+
0 commit comments