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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright 2022-2024 Mario Finelli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Advent of Code 2022 Day 16: <https://adventofcode.com/2022/day/16>
//!
//! TODO

use regex::Regex;
use std::collections::{BinaryHeap, HashMap, HashSet};

/// The solution for the day sixteen challenge.
///
/// TODO
///
/// # Example
/// ```rust
/// # use aoc::y22d16::y22d16;
/// ```
pub fn y22d16(input: &str, part: u32) -> u32 {
    let lines: Vec<_> = input.lines().collect();
    let flow_regex = Regex::new(r"^rate=(\d+);$").unwrap();

    let mut rates: HashMap<String, u32> = HashMap::new();
    let mut connections: HashMap<String, Vec<String>> = HashMap::new();
    let mut distances: HashMap<String, HashMap<String, Option<i32>>> =
        HashMap::new();

    for line in lines {
        let parts: Vec<_> = line.split_whitespace().collect();
        let flow_captures = flow_regex.captures(parts[4]).unwrap();

        let mut room_connections = Vec::new();

        for i in 9..parts.len() {
            room_connections.push(parts[i].trim_end_matches(',').to_string());
        }

        rates.insert(parts[1].to_string(), flow_captures[1].parse().unwrap());
        connections.insert(parts[1].to_string(), room_connections);
    }

    for (room, _rate) in &rates {
        let mut other_distances: HashMap<String, Option<i32>> = HashMap::new();

        for (other_room, _other_rate) in &rates {
            if room == other_room {
                other_distances.insert(other_room.to_string(), Some(0));
            } else if connections[room].contains(other_room) {
                other_distances.insert(other_room.to_string(), Some(1));
            } else {
                other_distances.insert(other_room.to_string(), None);
            }
        }

        distances.insert(room.to_string(), other_distances);
    }

    // now compute the distance from every node to every other node
    for (k, _) in &rates {
        for (i, _) in &rates {
            for (j, _) in &rates {
                let ij = distances[i][j];
                let ik = distances[i][k];
                let kj = distances[k][j];

                match ij {
                    Some(ij) => {
                        match ik {
                            Some(ik) => {
                                match kj {
                                    Some(kj) => {
                                        if ij > ik + kj {
                                            distances.get_mut(i).map(|val| {
                                                val.insert(
                                                    j.to_string(),
                                                    Some(ik + kj),
                                                )
                                            });
                                        }
                                    }
                                    None => {
                                        // kj is infinity so ik + kj is
                                        // infinity which is always greater
                                        // than ij (even if that's infinity)
                                    }
                                }
                            }
                            None => {
                                // ik is infinity so ik + kj is infinity which
                                // is always greater than ij (even if that's
                                // infinity)
                            }
                        }
                    }
                    None => {
                        match ik {
                            Some(ik) => {
                                match kj {
                                    Some(kj) => {
                                        // ik and kj are _not_ infinity but ij
                                        // _is_ so ik + kj will always be less
                                        distances.get_mut(i).map(|val| {
                                            val.insert(
                                                j.to_string(),
                                                Some(ik + kj),
                                            )
                                        });
                                    }
                                    None => {
                                        // kj is infinity so ik + kj is
                                        // infinity which is always greater
                                        // than ij (even if that's infinity)
                                    }
                                }
                            }
                            None => {
                                // ik is infinity so ik + kj is infinity which
                                // is always greater than ij (even if that's
                                // infinity)
                            }
                        }
                    }
                }
            }
        }
    }

    let mut positive_flows: HashSet<String> = HashSet::new();
    for (room, rate) in &rates {
        if *rate > 0 {
            positive_flows.insert(room.to_string());
        }
    }

    if part == 1 {
        dfs(
            &distances,
            &rates,
            positive_flows,
            "AA".to_string(),
            30,
            false,
        )
    } else {
        dfs(
            &distances,
            &rates,
            positive_flows,
            "AA".to_string(),
            26,
            true,
        )
    }
}

fn dfs(
    distances: &HashMap<String, HashMap<String, Option<i32>>>,
    rates: &HashMap<String, u32>,
    positive_flows: HashSet<String>,
    current_room: String,
    time_remaining: i32,
    help: bool,
) -> u32 {
    let mut paths: BinaryHeap<u32> = BinaryHeap::new();
    paths.push(0); // TODO: remove

    for room in &positive_flows {
        let new_time =
            time_remaining - (distances[&current_room][room].unwrap() + 1);
        let mut positive_flows = positive_flows.clone();
        positive_flows.remove(room);

        if time_remaining > new_time && new_time > 0 {
            let new_time_pos: u32 = new_time.try_into().unwrap();
            let rate = rates[room] * new_time_pos
                + dfs(
                    distances,
                    rates,
                    positive_flows,
                    room.to_string(),
                    new_time,
                    help,
                );
            paths.push(rate);
        }
    }

    // with the given rooms remaining, calculate most pressure that could be
    // released by the other person (elephant)
    if help {
        paths.push(dfs(
            distances,
            rates,
            positive_flows,
            "AA".to_string(),
            26,
            false,
        ));
    }

    paths.pop().unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn it_works() {
        let input = concat!(
            "Valve AA has flow rate=0; tunnels lead to valves DD, II, BB\n",
            "Valve BB has flow rate=13; tunnels lead to valves CC, AA\n",
            "Valve CC has flow rate=2; tunnels lead to valves DD, BB\n",
            "Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE\n",
            "Valve EE has flow rate=3; tunnels lead to valves FF, DD\n",
            "Valve FF has flow rate=0; tunnels lead to valves EE, GG\n",
            "Valve GG has flow rate=0; tunnels lead to valves FF, HH\n",
            "Valve HH has flow rate=22; tunnel leads to valve GG\n",
            "Valve II has flow rate=0; tunnels lead to valves AA, JJ\n",
            "Valve JJ has flow rate=21; tunnel leads to valve II\n",
        );

        assert_eq!(y22d16(input, 1), 1651);
        assert_eq!(y22d16(input, 2), 1707);
    }

    #[test]
    #[ignore]
    fn the_solution() {
        let contents = fs::read_to_string("input/2022/day16.txt").unwrap();

        assert_eq!(y22d16(&contents, 1), 2124);
        // TODO: optimize this... it takes almost 10m even with a release build
        // assert_eq!(y22d16(&contents, 2), 2775);
    }
}