Решение на Домашно 1 - търсене на съкровища от Георги Илиев
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 4 успешни тест(а)
- 1 неуспешни тест(а)
Код
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
mpsc::{self, Sender, Receiver},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TreasureLoc {
pub lane_index: usize,
pub cell_coord: usize,
pub value: i32,
}
#[derive(Debug, PartialEq, Eq)]
pub enum FoundTreasures {
Big(TreasureLoc),
Small(Vec<TreasureLoc>),
Nothing,
}
pub struct Scan<'a> {
// coord of first element in `cells`
pub start_coord: usize,
pub cells: &'a [i32]
}
pub struct Drone {
lane_index: usize,
sender: Sender<TreasureLoc>,
stop_flag: Arc<AtomicBool>
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
let mut best_treasure: Option<TreasureLoc> = None;
for scan in scanner {
if self.stop_flag.load(Ordering::Relaxed) {
break;
}
for (offset, &value) in scan.cells.iter().enumerate() {
if self.stop_flag.load(Ordering::Relaxed) {
break;
}
if value > 0 {
let loc = TreasureLoc {
lane_index: self.lane_index,
cell_coord: scan.start_coord + offset,
value,
};
if value >= 999 {
let _ = self.sender.send(loc);
return;
}
match &best_treasure {
None => best_treasure = Some(loc),
Some(current) if value > current.value => best_treasure = Some(loc),
_ => {}
}
}
}
}
if let Some(treasure) = best_treasure {
let _ = self.sender.send(treasure);
}
}
}
pub struct DroneController {
receiver: Receiver<TreasureLoc>,
sender: Option<Sender<TreasureLoc>>,
stop_flag: Arc<AtomicBool>
}
impl DroneController {
pub fn new() -> Self {
let (sender, receiver) = mpsc::channel();
Self {
receiver,
sender: Some(sender),
stop_flag: Arc::new(AtomicBool::new(false)),
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
Drone {
lane_index,
sender: self.sender.as_ref().expect("Cannot create drone after run() has been called").clone(),
stop_flag: self.stop_flag.clone(),
}
}
pub fn run(&mut self) -> FoundTreasures {
self.sender = None;
let mut small_treasures: Vec<TreasureLoc> = Vec::new();
let mut sum_small = 0;
while let Ok(treasure) = self.receiver.recv() {
if treasure.value >= 999 {
self.stop_flag.store(true, Ordering::Relaxed);
return FoundTreasures::Big(treasure);
}
sum_small += treasure.value;
small_treasures.push(treasure);
if sum_small >= 300 {
self.stop_flag.store(true, Ordering::Relaxed);
return FoundTreasures::Small(small_treasures);
}
}
if small_treasures.is_empty() {
FoundTreasures::Nothing
} else {
FoundTreasures::Small(small_treasures)
}
}
}
const fn assert_send_static<T: Send + 'static>() {}
const _: () = assert_send_static::<DroneController>();
const _: () = assert_send_static::<Drone>();
Лог от изпълнението
Updating crates.io index
Locking 46 packages to latest compatible versions
Compiling proc-macro2 v1.0.104
Compiling unicode-ident v1.0.22
Compiling quote v1.0.42
Compiling libc v0.2.178
Compiling syn v2.0.111
Compiling parking_lot_core v0.9.12
Compiling pin-project-lite v0.2.16
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-io v0.3.31
Compiling smallvec v1.15.1
Compiling cfg-if v1.0.4
Compiling futures-task v0.3.31
Compiling pin-utils v0.1.0
Compiling scopeguard v1.2.0
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling lock_api v0.4.14
Compiling errno v0.3.14
Compiling signal-hook-registry v1.4.8
Compiling parking_lot v0.12.5
Compiling mio v1.1.1
Compiling futures-macro v0.3.31
Compiling tokio-macros v2.6.0
Compiling socket2 v0.6.1
Compiling futures-util v0.3.31
Compiling bytes v1.11.0
Compiling tokio v1.48.0
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
Compiling solution v0.1.0 (/tmp/d20251229-4108951-4f8tjb/solution)
warning: function `assert_send_static` is never used
--> src/lib.rs:127:10
|
127 | const fn assert_send_static<T: Send + 'static>() {}
| ^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (lib) generated 1 warning
Finished `test` profile [unoptimized + debuginfo] target(s) in 17.89s
Running tests/solution_test.rs (target/debug/deps/solution_test-f512224d9fb3caf8)
running 5 tests
test solution_test::test_big_treasure ... ok
test solution_test::test_nothing ... FAILED
test solution_test::test_small_treasure ... ok
test solution_test::test_small_treasure_2 ... ok
test solution_test::test_return_immediately_when_found ... ok
failures:
---- solution_test::test_nothing stdout ----
thread 'solution_test::test_nothing' panicked at tests/solution_test.rs:174:5:
assertion `left == right` failed
left: Small([TreasureLoc { lane_index: 1, cell_coord: 2, value: 10 }, TreasureLoc { lane_index: 0, cell_coord: 2, value: 10 }])
right: Nothing
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
solution_test::test_nothing
test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.10s
error: test failed, to rerun pass `--test solution_test`
