main
Usmann Khan 2022-12-16 23:52:37 +00:00
parent e3641e144d
commit 224ed7e7c4
1 changed files with 24 additions and 11 deletions

View File

@ -72,17 +72,7 @@ fn run() {
}
fn process_part_2<'a, T>(lines: T) where T: IntoIterator<Item = &'a str> {
let (sensors, _) = parse(lines);
let mut worker_bundles = HashMap::new();
for worker in 0..99 {
let mut work_item = Vec::new();
for sensor in &sensors {
let chunk_s = sensor.visibility / 100;
// let remainder = sensor.visibility - (99*chunk_s); <1% chance this worker contains solution.. ignore for now
work_item.push(chunk_s);
}
worker_bundles.insert(worker, work_item);
};
let (sensors, worker_bundles) = prep_work(lines);
let mut handles = Vec::new();
let bundle_storage = Arc::new(worker_bundles);
@ -97,6 +87,29 @@ fn process_part_2<'a, T>(lines: T) where T: IntoIterator<Item = &'a str> {
handles.into_iter().for_each(|handle|{ handle.join();});
}
fn prep_work<'a, T>(lines: T) -> (Vec<Sensor>, HashMap<isize, Vec<usize>>) where T: IntoIterator<Item = &'a str> {
let (sensors, _) = parse(lines);
let mut worker_bundles = HashMap::new();
for worker in 0..99 {
let mut work_item = Vec::new();
for sensor in &sensors {
let chunk_s = sensor.visibility / 100;
work_item.push(chunk_s);
}
worker_bundles.insert(worker, work_item);
};
// remainder worker
let worker = 99;
let mut work_item = Vec::new();
for sensor in &sensors {
let chunk_s = sensor.visibility / 100;
let remainder = sensor.visibility - (99*chunk_s);
work_item.push(remainder);
}
worker_bundles.insert(worker, work_item);
(sensors, worker_bundles)
}
fn do_part2_work(worker_bundles: Arc<HashMap<isize, Vec<usize>>>, worker: isize, bounds: &RangeInclusive<isize>, sensors: Arc<Vec<Sensor>>) {
worker_bundles.get(&worker).and_then(
|x| {