First attempt at day 16
This commit is contained in:
58
src/bin/d16p1.rs
Normal file
58
src/bin/d16p1.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user