2022 Day 16 Part 2
This commit is contained in:
parent
6c645a607d
commit
362d843997
1 changed files with 22 additions and 8 deletions
|
|
@ -8,13 +8,10 @@ use regex::Regex;
|
||||||
const INPUT: &str = include_str!("../../input/day16.txt");
|
const INPUT: &str = include_str!("../../input/day16.txt");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{}", part1(INPUT));
|
let graph = Graph::parse(INPUT);
|
||||||
}
|
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
println!("{}", graph.max_flow(30).flow);
|
||||||
let graph = Graph::parse(input);
|
println!("{}", graph.max_flow_with_an_elephriend(26));
|
||||||
|
|
||||||
graph.max_flow(30)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// I really want this to be Copy, so here's a hack to avoid strings.
|
// I really want this to be Copy, so here's a hack to avoid strings.
|
||||||
|
|
@ -206,7 +203,7 @@ impl Ord for State {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Graph {
|
impl Graph {
|
||||||
fn max_flow(&self, total_minutes: usize) -> Flow {
|
fn max_flow(&self, total_minutes: usize) -> State {
|
||||||
let paths = {
|
let paths = {
|
||||||
let nodes: Vec<Valve> = self.valves.keys().cloned().collect();
|
let nodes: Vec<Valve> = self.valves.keys().cloned().collect();
|
||||||
let mut edges: HashMap<(Valve, Valve), u64> = HashMap::new();
|
let mut edges: HashMap<(Valve, Valve), u64> = HashMap::new();
|
||||||
|
|
@ -256,7 +253,24 @@ impl Graph {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
best.flow
|
best
|
||||||
|
}
|
||||||
|
|
||||||
|
fn max_flow_with_an_elephriend(&self, total_minutes: usize) -> Flow {
|
||||||
|
let best = self.max_flow(total_minutes);
|
||||||
|
|
||||||
|
let elegraph = {
|
||||||
|
let mut g = self.clone();
|
||||||
|
for action in best.path.0 {
|
||||||
|
if let Action::Open(v) = action {
|
||||||
|
g.valves.insert(v, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g
|
||||||
|
};
|
||||||
|
let elebest = elegraph.max_flow(total_minutes);
|
||||||
|
|
||||||
|
best.flow + elebest.flow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue