@@ -28,11 +28,54 @@ class Day16 extends Day
28
28
public function solvePart1 (mixed $ input ): int |string |null
29
29
{
30
30
$ input = $ this ->parseInput ($ input );
31
- dd ($ input );
31
+ $ minute = 0 ;
32
+ $ pressure = 0 ;
33
+ $ openValves = [];
34
+ $ valve = $ input ->first ();
35
+ $ openValve = false ;
36
+ while ($ minute < 30 ) {
37
+ ++$ minute ;
38
+ printf ("Minute: %d current pressure: %d current valve: %s open valves: %s \n" , $ minute , $ pressure , $ valve ['valve ' ], implode (', ' , $ openValves ));
39
+ if ($ openValve ) {
40
+ printf ("Opening %s releasing %d pressure \n" , $ openValve ['valve ' ], $ openValve ['flow_rate ' ]);
41
+ $ openValves [] = $ openValve ['valve ' ];
42
+ $ valve = $ openValve ;
43
+ $ openValve = false ;
44
+ continue ;
45
+ }
46
+
47
+ // otherwise find the next valve with the highest flow rate
48
+ $ nextValve = $ valve ['tunnels ' ]->filter (fn (array $ tunnel ): bool => !in_array ($ tunnel ['valve ' ], $ openValves ))->first ();
49
+
50
+ if ($ valve ['flow_rate ' ] < $ nextValve ['flow_rate ' ]) {
51
+ printf ("Moving to %s \n" , $ nextValve ['valve ' ]);
52
+ $ valve = $ nextValve ;
53
+ $ openValves [] = $ valve ['valve ' ];
54
+ $ open = true ;
55
+
56
+ } else {
57
+ printf ("Opening %s \n" , $ valve ['valve ' ]);
58
+ $ openValves [] = $ valve ['valve ' ];
59
+ $ pressure += $ valve ['flow_rate ' ];
60
+ $ valve = null ;
61
+ }
62
+ $ pressure += array_sum (array_map (fn (string $ valve ): int => $ input [$ valve ]['flow_rate ' ], $ openValves ));
63
+ printf ("Minute: %d current pressure: %d current valve: %s open valves: %s \n" , $ minute , $ pressure , $ valve ['valve ' ], implode (', ' , $ openValves ));
64
+ dd ($ openValves );
65
+ // take the valve with the highest flow rate by shifting and getting the first key
66
+ }
67
+
68
+ dd ($ pressure );
69
+
32
70
33
71
return null ;
34
72
}
35
73
74
+ protected function tunnelWithHighestFlowRate (array $ valves , array $ openValves ): array
75
+ {
76
+ return $ valves ->filter (fn (array $ valve ): bool => !in_array ($ valve ['valve ' ], $ openValves ))->first ();
77
+ }
78
+
36
79
/**
37
80
* Solve Part 2 of the day's problem.
38
81
*/
@@ -47,28 +90,34 @@ public function solvePart2(mixed $input): int|string|null
47
90
48
91
/**
49
92
* Parse the input data.
50
- * @return Collection<int, array<string, string|int|array<int, string>>>
93
+ *
94
+ * @return Collection<string, array<string, int|Collection<int, string>>>
51
95
*/
52
96
protected function parseInput (mixed $ input ): Collection
53
97
{
54
- $ input = is_array ($ input ) ? $ input : explode ("\n" , $ input );
55
-
56
- return collect ($ input )
57
- ->map (function (string $ line ): array {
58
- $ valve = null ;
59
- $ flowRate = null ;
60
- $ tunnels = null ;
61
- if (preg_match ('/Valve (\w+) has flow rate=(\d+); tunnels? leads? to valves? (.+)/ ' , $ line , $ matches )) {
62
- $ valve = $ matches [1 ];
63
- $ flowRate = (int )$ matches [2 ];
64
- $ tunnels = explode (', ' , $ matches [3 ]);
65
- }
98
+ return collect (is_array ($ input ) ? $ input : explode ("\n" , $ input ))
99
+ ->mapWithKeys (function (string $ line ): array {
100
+ if (preg_match ('/Valve (\w+) has flow rate=(\d+); tunnels? leads? to valves? (.+)/ ' , $ line , $ matches )) {
101
+ [, $ valve , $ flowRate , $ tunnels ] = $ matches ;
66
102
return [
67
- 'valve ' => $ valve ,
68
- 'flow_rate ' => $ flowRate ,
69
- 'tunnels ' => $ tunnels ,
103
+ $ valve => [
104
+ 'valve ' => $ valve ,
105
+ 'flow_rate ' => (int ) $ flowRate ,
106
+ 'tunnels ' => explode (', ' , $ tunnels ),
107
+ ]
70
108
];
71
- })
72
- ;
109
+ }
110
+ return [];
111
+ });
112
+
113
+ /* return $valves->map(function (array $valve) use ($valves) {
114
+ $valve['tunnels'] = collect($valve['tunnels'])
115
+ ->map(fn (string $tunnel): array => ['valve' => $tunnel, 'flow_rate' => $valves[$tunnel]['flow_rate']])
116
+ ->sortByDesc(fn (array $tunnel): int => $tunnel['flow_rate'])
117
+ //->toArray();
118
+ ;
119
+
120
+ return $valve;
121
+ }); */
73
122
}
74
123
}
0 commit comments