|
38 | 38 | //! Then we check every possible pair formed by those values, considering only the pairs |
39 | 39 | //! where the sets of valves are [disjoint](https://en.wikipedia.org/wiki/Disjoint_sets), |
40 | 40 | //! which is when you and the elephant have visited different sets of valves. |
| 41 | +use crate::util::bitset::*; |
41 | 42 | use crate::util::hash::*; |
42 | 43 | use crate::util::parse::*; |
43 | 44 | use std::cmp::Ordering; |
@@ -264,24 +265,19 @@ pub fn part2(input: &Input) -> u32 { |
264 | 265 | fn explore(input: &Input, state: &State, high_score: &mut impl FnMut(usize, u32) -> u32) { |
265 | 266 | let State { todo, from, time, pressure } = *state; |
266 | 267 | let score = high_score(todo, pressure); |
267 | | - let mut valves = todo; |
268 | | - |
269 | | - while valves > 0 { |
270 | | - // Stores the set of unopened valves in a single integer as a bit mask with a 1 |
271 | | - // for each unopened valve. This code iterates over each valve by finding the lowest |
272 | | - // 1 bit then removing it from the set. |
273 | | - let to = valves.trailing_zeros() as usize; |
274 | | - let mask = 1 << to; |
275 | | - valves ^= mask; |
276 | 268 |
|
| 269 | + // Stores the set of unopened valves in a single integer as a bit mask with a 1 |
| 270 | + // for each unopened valve. This code iterates over each valve by finding the lowest |
| 271 | + // 1 bit then removing it from the set. |
| 272 | + for to in todo.biterator() { |
277 | 273 | // Check if there's enough time to reach the valve. |
278 | 274 | let needed = input.distance[from * input.size + to]; |
279 | 275 | if needed >= time { |
280 | 276 | continue; |
281 | 277 | } |
282 | 278 |
|
283 | 279 | // Calculate the total pressure released by a valve up front. |
284 | | - let todo = todo ^ mask; |
| 280 | + let todo = todo ^ (1 << to); |
285 | 281 | let time = time - needed; |
286 | 282 | let pressure = pressure + time * input.flow[to]; |
287 | 283 |
|
|
0 commit comments