First attempt at day 16

This commit is contained in:
jazzpi
2022-12-20 00:44:56 +01:00
parent 0d30968d3e
commit 46d409fa92
3 changed files with 258 additions and 0 deletions

58
src/bin/d16p1.rs Normal file
View File

@ -0,0 +1,58 @@
use std::{
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
},
thread,
};
use aoc22::{day16, util};
pub fn main() {
let valves = day16::parse_valves(&util::parse_input());
let dists = day16::calc_dists(&valves);
let state = day16::State::new(Arc::new(valves), Arc::new(dists));
let possible_states = Arc::new(Mutex::new(Vec::new()));
possible_states.lock().unwrap().push(state);
let lower_bound = Arc::new(AtomicUsize::new(0));
let mut handles = Vec::new();
for _ in 0..16 {
let s = possible_states.clone();
let l = lower_bound.clone();
handles.push(thread::spawn(move || check_states(s, l)));
}
for handle in handles {
handle.join().unwrap();
}
println!(
"Most pressure released is {}",
lower_bound.load(Ordering::Relaxed)
);
}
pub fn check_states(possible_states: Arc<Mutex<Vec<day16::State>>>, lower_bound: Arc<AtomicUsize>) {
loop {
let state = { possible_states.lock().unwrap().pop() };
if state.is_none() {
break;
}
let state = state.unwrap();
if state.finished() {
let score = state.lower_bound();
lower_bound.fetch_max(score, Ordering::Relaxed);
} else {
let x = state.possible_actions();
for action in x {
if action.upper_bound() > lower_bound.load(Ordering::Relaxed) {
possible_states.lock().unwrap().push(action);
}
}
}
}
}