|
| 1 | +"""Day 16: Proboscidea Volcanium |
| 2 | +
|
| 3 | +This module provides the solution for Advent of Code 2022 - Day 16. |
| 4 | +
|
| 5 | +It models valve opening optimization in a volcanic tunnel network, using |
| 6 | +dynamic programming to maximize pressure release within time constraints. |
| 7 | +
|
| 8 | +The module contains a cached helper function and a Solution class that |
| 9 | +inherits from SolutionBase for parsing valve networks and computing optimal |
| 10 | +valve opening sequences. |
| 11 | +""" |
| 12 | + |
| 13 | +from functools import cache |
| 14 | +import re |
| 15 | +from typing import ClassVar |
| 16 | + |
| 17 | +import rustworkx as rx |
| 18 | + |
| 19 | +from aoc.models.base import SolutionBase |
| 20 | + |
| 21 | + |
| 22 | +@cache |
| 23 | +def _max_pressure_helper( |
| 24 | + current_valve: str, |
| 25 | + time_left: int, |
| 26 | + unopened: frozenset[str], |
| 27 | + flow_rates: tuple[tuple[str, int], ...], |
| 28 | + distances: tuple[tuple[tuple[str, str], int], ...], |
| 29 | +) -> int: |
| 30 | + """Calculate maximum pressure releasable from current state using memoization. |
| 31 | +
|
| 32 | + Recursively explores all possible valve opening sequences, caching results |
| 33 | + to avoid recomputation of identical states. Uses immutable types for caching. |
| 34 | +
|
| 35 | + Args: |
| 36 | + current_valve: Current position in valve network |
| 37 | + time_left: Remaining minutes before eruption |
| 38 | + unopened: Frozenset of valves not yet opened |
| 39 | + flow_rates: Tuple of (valve, rate) pairs for caching |
| 40 | + distances: Tuple of ((valve1, valve2), distance) pairs for caching |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + int: Maximum pressure that can be released from this state |
| 45 | + """ |
| 46 | + if time_left <= 0 or not unopened: |
| 47 | + return 0 |
| 48 | + |
| 49 | + flow_rates_dict = dict(flow_rates) |
| 50 | + distances_dict = dict(distances) |
| 51 | + |
| 52 | + best = 0 |
| 53 | + |
| 54 | + for valve in unopened: |
| 55 | + travel_time = distances_dict[(current_valve, valve)] |
| 56 | + time_after_opening = time_left - travel_time - 1 |
| 57 | + |
| 58 | + if time_after_opening > 0: |
| 59 | + pressure = flow_rates_dict[valve] * time_after_opening |
| 60 | + remaining = unopened - {valve} |
| 61 | + |
| 62 | + total = pressure + _max_pressure_helper( |
| 63 | + valve, time_after_opening, remaining, flow_rates, distances |
| 64 | + ) |
| 65 | + best = max(best, total) |
| 66 | + |
| 67 | + return int(best) |
| 68 | + |
| 69 | + |
| 70 | +class Solution(SolutionBase): |
| 71 | + """Optimize valve opening sequence to maximize pressure release in volcano. |
| 72 | +
|
| 73 | + This solution models a tunnel network with pressure-release valves. Part 1 |
| 74 | + finds the optimal sequence to open valves alone in 30 minutes. Part 2 solves |
| 75 | + for two agents (you and an elephant) working in parallel for 26 minutes. |
| 76 | +
|
| 77 | + Uses graph algorithms to precompute shortest paths between important valves, |
| 78 | + then dynamic programming with memoization to explore valve opening sequences. |
| 79 | + """ |
| 80 | + |
| 81 | + VALVE_REGEX: ClassVar[re.Pattern[str]] = re.compile(r"([A-Z]{2})") |
| 82 | + FLOW_RATE_REGEX: ClassVar[re.Pattern[str]] = re.compile(r"rate=(\d+)") |
| 83 | + |
| 84 | + def parse_data( |
| 85 | + self, data: list[str] |
| 86 | + ) -> tuple[tuple[tuple[str, int], ...], tuple[tuple[tuple[str, str], int], ...]]: |
| 87 | + """Parse valve network into flow rates and distances between valves. |
| 88 | +
|
| 89 | + Constructs a graph of tunnel connections and uses Dijkstra's algorithm |
| 90 | + to compute shortest paths between all important valves (AA start position |
| 91 | + and any valve with positive flow rate). |
| 92 | +
|
| 93 | + Args: |
| 94 | + data: List of strings describing valves and tunnel connections |
| 95 | +
|
| 96 | + Returns |
| 97 | + ------- |
| 98 | + tuple: Immutable flow_rates mapping and distances mapping for caching |
| 99 | + """ |
| 100 | + flow_rates: dict[str, int] = {} |
| 101 | + distances: dict[tuple[str, str], int] = {} |
| 102 | + |
| 103 | + graph: rx.PyGraph = rx.PyGraph() |
| 104 | + valve_to_idx: dict[str, int] = {} |
| 105 | + idx_to_valve: dict[int, str] = {} |
| 106 | + |
| 107 | + for line in data: |
| 108 | + valve_match = re.search(self.VALVE_REGEX, line) |
| 109 | + rate_match = re.search(self.FLOW_RATE_REGEX, line) |
| 110 | + |
| 111 | + if valve_match is None or rate_match is None: |
| 112 | + err_msg = f"Invalid line format: {line}" |
| 113 | + raise ValueError(err_msg) |
| 114 | + |
| 115 | + valve = valve_match.group(1) |
| 116 | + rate = int(rate_match.group(1)) |
| 117 | + |
| 118 | + idx = graph.add_node(valve) |
| 119 | + valve_to_idx[valve] = idx |
| 120 | + idx_to_valve[idx] = valve |
| 121 | + |
| 122 | + if rate > 0: |
| 123 | + flow_rates[valve] = rate |
| 124 | + |
| 125 | + for line in data: |
| 126 | + valve_match = re.search(self.VALVE_REGEX, line) |
| 127 | + if valve_match is None: |
| 128 | + err_msg = f"Invalid line format: {line}" |
| 129 | + raise ValueError(err_msg) |
| 130 | + |
| 131 | + valve = valve_match.group(1) |
| 132 | + tunnels = re.findall(r"[A-Z]{2}", line.split("valve")[-1]) |
| 133 | + valve_idx = valve_to_idx[valve] |
| 134 | + |
| 135 | + for neighbor in tunnels: |
| 136 | + graph.add_edge(valve_idx, valve_to_idx[neighbor], 1) |
| 137 | + |
| 138 | + important = ["AA", *list(flow_rates.keys())] |
| 139 | + |
| 140 | + for idx, v1 in enumerate(important): |
| 141 | + for v2 in important[idx + 1 :]: |
| 142 | + idx1, idx2 = valve_to_idx[v1], valve_to_idx[v2] |
| 143 | + |
| 144 | + dist = rx.dijkstra_shortest_path_lengths( |
| 145 | + graph, idx1, edge_cost_fn=lambda _: 1, goal=idx2 |
| 146 | + )[idx2] |
| 147 | + |
| 148 | + distances[(v1, v2)] = distances[(v2, v1)] = dist |
| 149 | + |
| 150 | + return tuple(flow_rates.items()), tuple(distances.items()) |
| 151 | + |
| 152 | + def dfs( |
| 153 | + self, |
| 154 | + current: str, |
| 155 | + time: int, |
| 156 | + unopened: frozenset[str], |
| 157 | + opened: frozenset[str], |
| 158 | + pressure: int, |
| 159 | + flow_rates: dict[str, int], |
| 160 | + distances: dict[tuple[str, str], int], |
| 161 | + results: dict[frozenset[str], int], |
| 162 | + ) -> None: |
| 163 | + """Depth-first search to find all reachable valve combinations and pressures. |
| 164 | +
|
| 165 | + Explores all possible paths through the valve network, recording the best |
| 166 | + pressure achievable for each unique set of opened valves. Used in Part 2 |
| 167 | + to find complementary valve sets for parallel agents. |
| 168 | +
|
| 169 | + Args: |
| 170 | + current: Current valve position |
| 171 | + time: Remaining time |
| 172 | + unopened: Set of valves not yet opened |
| 173 | + opened: Set of valves already opened |
| 174 | + pressure: Total pressure released so far |
| 175 | + flow_rates: Valve flow rate mappings |
| 176 | + distances: Shortest path distances between valves |
| 177 | + results: Dictionary to accumulate (opened_set -> best_pressure) mappings |
| 178 | + """ |
| 179 | + if opened not in results or pressure > results[opened]: |
| 180 | + results[opened] = pressure |
| 181 | + |
| 182 | + for valve in unopened: |
| 183 | + travel_time = distances[(current, valve)] |
| 184 | + time_after = time - travel_time - 1 |
| 185 | + |
| 186 | + if time_after > 0: |
| 187 | + valve_pressure = flow_rates[valve] * time_after |
| 188 | + self.dfs( |
| 189 | + valve, |
| 190 | + time_after, |
| 191 | + unopened - {valve}, |
| 192 | + opened | {valve}, |
| 193 | + pressure + valve_pressure, |
| 194 | + flow_rates, |
| 195 | + distances, |
| 196 | + results, |
| 197 | + ) |
| 198 | + |
| 199 | + def part1(self, data: list[str]) -> int: |
| 200 | + """Find maximum pressure releasable by opening valves alone in 30 minutes. |
| 201 | +
|
| 202 | + Starting at valve AA with 30 minutes, determines the optimal sequence |
| 203 | + of valve openings to maximize total pressure released before volcanic |
| 204 | + eruption. Travel between valves and opening each valve costs 1 minute. |
| 205 | +
|
| 206 | + Args: |
| 207 | + data: List of strings describing valve network |
| 208 | +
|
| 209 | + Returns |
| 210 | + ------- |
| 211 | + int: Maximum total pressure that can be released |
| 212 | + """ |
| 213 | + flow_rates, distances = self.parse_data(data) |
| 214 | + |
| 215 | + return _max_pressure_helper( |
| 216 | + "AA", 30, frozenset(dict(flow_rates).keys()), flow_rates, distances |
| 217 | + ) |
| 218 | + |
| 219 | + def part2(self, data: list[str]) -> int: |
| 220 | + """Find maximum pressure with you and elephant working in parallel for 26 minutes. |
| 221 | +
|
| 222 | + After spending 4 minutes teaching the elephant, you both have 26 minutes |
| 223 | + to open valves. Finds the optimal division of valves between two agents |
| 224 | + by exploring all possible combinations and selecting non-overlapping sets |
| 225 | + with maximum combined pressure. |
| 226 | +
|
| 227 | + Args: |
| 228 | + data: List of strings describing valve network |
| 229 | +
|
| 230 | + Returns |
| 231 | + ------- |
| 232 | + int: Maximum combined pressure achievable with two parallel agents |
| 233 | + """ |
| 234 | + flow_rates, distances = self.parse_data(data) |
| 235 | + all_valves = frozenset(dict(flow_rates).keys()) |
| 236 | + |
| 237 | + results: dict[frozenset[str], int] = {frozenset(): 0} |
| 238 | + |
| 239 | + self.dfs("AA", 26, all_valves, frozenset(), 0, dict(flow_rates), dict(distances), results) |
| 240 | + |
| 241 | + max_pressure = 0 |
| 242 | + items = list(results.items()) |
| 243 | + |
| 244 | + for i in range(len(items)): |
| 245 | + for j in range(i, len(items)): |
| 246 | + combo1, pressure1 = items[i] |
| 247 | + combo2, pressure2 = items[j] |
| 248 | + |
| 249 | + if not combo1 & combo2: |
| 250 | + max_pressure = max(max_pressure, pressure1 + pressure2) |
| 251 | + |
| 252 | + return int(max_pressure) |
0 commit comments