Решение на Домашно 1 - търсене на съкровища от Йосиф Хамед

Обратно към всички решения

Към профила на Йосиф Хамед

Резултати

  • 16 точки от тестове
  • 0 бонус точки
  • 16 точки общо
  • 4 успешни тест(а)
  • 1 неуспешни тест(а)

Код

use std::{
collections::HashMap,
sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, Sender},
Arc,
},
};
const fn assert_send_static<T: Send + 'static>() {}
const _: () = assert_send_static::<DroneController>();
const _: () = assert_send_static::<Drone>();
#[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 {
// responsible for sending ata to the controller
coms: Sender<TreasureLoc>,
should_stop: Arc<AtomicBool>,
lane_index: usize,
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
for scan in scanner {
if self.should_stop.load(Ordering::Relaxed) {
break;
}
for (i, cell) in scan.cells.iter().enumerate() {
let _ = self.coms.send(TreasureLoc {
lane_index: self.lane_index,
cell_coord: i + scan.start_coord,
value: *cell,
});
}
}
}
}
pub struct DroneController {
found_treasures: HashMap<usize, TreasureLoc>,
receiver: Receiver<TreasureLoc>,
sender: Option<Sender<TreasureLoc>>,
should_stop: Arc<AtomicBool>,
}
impl DroneController {
pub fn new() -> Self {
let (tx, rx): (mpsc::Sender<TreasureLoc>, mpsc::Receiver<TreasureLoc>) = mpsc::channel();
DroneController {
found_treasures: HashMap::new(),
receiver: rx,
sender: Some(tx),
should_stop: Arc::new(AtomicBool::new(false)),
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
Drone {
coms: self.sender.clone().unwrap(),
should_stop: Arc::clone(&self.should_stop),
lane_index: lane_index,
}
}
pub fn run(&mut self) -> FoundTreasures {
self.sender = None;
while let Ok(loc) = self.receiver.recv() {
if loc.value >= 999 {
return FoundTreasures::Big(loc);
}
match self.found_treasures.get(&loc.lane_index) {
Some(found) => {
if found.value < loc.value {
self.found_treasures.insert(loc.lane_index, loc);
}
}
None => {
self.found_treasures.insert(loc.lane_index, loc);
}
}
let total_val = self
.found_treasures
.values()
.map(|f| f.value)
.fold(0, |a, b| a + b);
if total_val >= 300 {
let v: Vec<TreasureLoc> = self.found_treasures.values().cloned().collect();
return FoundTreasures::Small(v);
}
}
FoundTreasures::Nothing
}
}

Лог от изпълнението

Updating crates.io index
     Locking 46 packages to latest compatible versions
   Compiling proc-macro2 v1.0.104
   Compiling libc v0.2.178
   Compiling quote v1.0.42
   Compiling unicode-ident v1.0.22
   Compiling syn v2.0.111
   Compiling futures-sink v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling parking_lot_core v0.9.12
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling slab v0.4.11
   Compiling smallvec v1.15.1
   Compiling memchr v2.7.6
   Compiling futures-task v0.3.31
   Compiling scopeguard v1.2.0
   Compiling futures-io v0.3.31
   Compiling pin-utils v0.1.0
   Compiling cfg-if v1.0.4
   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 socket2 v0.6.1
   Compiling futures-macro v0.3.31
   Compiling tokio-macros v2.6.0
   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-1821574/solution)
warning: function `assert_send_static` is never used
  --> src/lib.rs:10:10
   |
10 | 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.57s
     Running tests/solution_test.rs (target/debug/deps/solution_test-f512224d9fb3caf8)

running 5 tests
test solution_test::test_nothing ... ok
test solution_test::test_big_treasure ... ok
test solution_test::test_small_treasure ... ok
test solution_test::test_small_treasure_2 ... ok
test solution_test::test_return_immediately_when_found ... FAILED

failures:

---- solution_test::test_return_immediately_when_found stdout ----
thread 'solution_test::test_return_immediately_when_found' panicked at tests/solution_test.rs:234:60:
test timeout
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_return_immediately_when_found

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Йосиф качи първо решение на 23.12.2025 01:20 (преди около 1 месеца)