@@ -14,6 +14,15 @@ public class Day16 extends Day2022 {
14
14
15
15
public Day16 () {
16
16
super (16 );
17
+ this .example = 1 ;
18
+ this .in = dayStream ().map (s -> {
19
+ try {
20
+ return readString (s , "Valve %s has flow rate=%n; tunnels lead to valves %s" , Valve .class );
21
+ } catch (IllegalStateException e ) {
22
+ return readString (s , "Valve %s has flow rate=%n; tunnel leads to valve %s" , Valve .class );
23
+ }
24
+ }).toList ();
25
+ this .indices = IntStream .range (0 , in .size ()).boxed ().collect (Collectors .toMap (i -> this .in .get (i ).name , i -> i ));
17
26
}
18
27
19
28
public static void main (String [] args ) {
@@ -26,24 +35,24 @@ public static void main(String[] args) {
26
35
// d.submitPart2();
27
36
}
28
37
38
+ private final List <Valve > in ;
39
+ private final Map <String , Integer > indices ;
40
+
29
41
public record Valve (String name , long flow , String others ) {}
30
- public record State (Map <String , Long > open , List < String > path , int index ) {}
42
+ public record State (Map <String , Long > open , int mins , int index ) {}
31
43
32
44
@ Override
33
45
public Object part1 () {
34
- List <Valve > in = dayStream ().map (s -> {
35
- try {
36
- return readString (s , "Valve %s has flow rate=%n; tunnels lead to valves %s" , Valve .class );
37
- } catch (IllegalStateException e ) {
38
- return readString (s , "Valve %s has flow rate=%n; tunnel leads to valve %s" , Valve .class );
39
- }
40
- }).toList ();
41
- Map <String , Integer > indices = IntStream .range (0 , in .size ()).boxed ().collect (Collectors .toMap (i -> this .in .get (i ).name , i -> i ));
42
- List <State > states = new ArrayList <>();
43
- return walk (0 , 0 , new HashMap <>(), new HashSet <>());
46
+ return walk (0 , 0 , new HashMap <>(), new HashMap <>());
44
47
}
45
48
46
- private long walk (int mins , int index , Map <String , Long > open , Set <String > visited ) {
49
+ Map <State , Long > visited = new HashMap <>();
50
+
51
+ private long walk (int mins , int index , Map <String , Long > open , Map <State , Long > visited ) {
52
+ State st = new State (open , mins , index );
53
+ if (visited .containsKey (st )){
54
+ return visited .get (st );
55
+ }
47
56
long flow = open .values ().stream ().mapToLong (e -> e ).sum ();
48
57
if (mins == 30 ){
49
58
// System.out.println(Arrays.toString(open.keySet().toArray()));
@@ -54,9 +63,11 @@ private long walk(int mins, int index, Map<String, Long> open, Set<String> visit
54
63
List <Long > options = new ArrayList <>();
55
64
if (v .flow > 0 && !open .containsKey (v .name )) {
56
65
open .put (v .name , v .flow );
57
- options .add (walk (mins + 1 , index , new HashMap <>(open ), new HashSet <>(visited )));
66
+ options .add (walk (mins + 1 , index , new HashMap <>(open ), new HashMap <>(visited )));
58
67
}
59
- return flow + LongStream .concat (options .stream ().mapToLong (e -> e ), Arrays .stream (v .others .split (", " )).mapToLong (s -> walk (mins + 1 , indices .get (s ), new HashMap <>(open ), new HashSet <>(visited )))).max ().orElse (0 );
68
+ long res = flow + LongStream .concat (options .stream ().mapToLong (e -> e ), Arrays .stream (v .others .split (", " )).mapToLong (s -> walk (mins + 1 , indices .get (s ), new HashMap <>(open ), new HashMap <>(visited )))).max ().orElse (0 );
69
+ visited .put (st , res );
70
+ return res ;
60
71
}
61
72
62
73
@ Override
0 commit comments