Решение на Домашно 1 - търсене на съкровища от Симеон Георгиев
Към профила на Симеон Георгиев
Резултати
- 12 точки от тестове
- 0 бонус точки
- 12 точки общо
- 3 успешни тест(а)
- 2 неуспешни тест(а)
Код
use std::sync::mpsc::{channel, Sender, Receiver};
use std::collections::HashMap;
#[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>,
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
for scan in scanner {
for (offset, treasure) in scan.cells.iter().enumerate() {
if *treasure > 0 {
let treasure_loc = TreasureLoc {
lane_index: self.lane_index,
cell_coord: scan.start_coord + offset,
value: *treasure,
};
if let Err(_) = self.sender.send(treasure_loc) {
return;
}
}
}
}
}
}
pub struct DroneController {
receiver: Receiver<TreasureLoc>,
sender: Sender<TreasureLoc>,
small_treasure_sum: i32,
drone_treasures: HashMap<usize, TreasureLoc>,
}
impl DroneController {
pub fn new() -> Self {
let (sender, receiver): (Sender<TreasureLoc>, Receiver<TreasureLoc>) = channel();
DroneController {
receiver: receiver,
sender: sender,
small_treasure_sum: 0,
drone_treasures: HashMap::new(),
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
Drone {
lane_index: lane_index,
sender: self.sender.clone(),
}
}
pub fn run(&mut self) -> FoundTreasures {
for treasure_loc in self.receiver.iter() {
if treasure_loc.value >= 999 {
return FoundTreasures::Big(treasure_loc)
} else {
self.small_treasure_sum += treasure_loc.value;
if let Some(old_loc) = self.drone_treasures.get(&treasure_loc.lane_index) {
if old_loc.value < treasure_loc.value {
self.small_treasure_sum -= old_loc.value;
self.drone_treasures.insert(treasure_loc.lane_index, treasure_loc);
}
} else {
self.drone_treasures.insert(treasure_loc.lane_index, treasure_loc);
}
if self.small_treasure_sum >= 300 {
let small_treasures: Vec<TreasureLoc> = self.drone_treasures.clone().into_values().collect();
return FoundTreasures::Small(small_treasures);
}
}
}
panic!();
}
}
Лог от изпълнението
Updating crates.io index
Locking 46 packages to latest compatible versions
Compiling proc-macro2 v1.0.104
Compiling libc v0.2.178
Compiling unicode-ident v1.0.22
Compiling quote v1.0.42
Compiling syn v2.0.111
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling parking_lot_core v0.9.12
Compiling pin-project-lite v0.2.16
Compiling futures-channel v0.3.31
Compiling slab v0.4.11
Compiling futures-task v0.3.31
Compiling cfg-if v1.0.4
Compiling scopeguard v1.2.0
Compiling smallvec v1.15.1
Compiling memchr v2.7.6
Compiling pin-utils v0.1.0
Compiling futures-io v0.3.31
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-1wmvtmr/solution)
Finished `test` profile [unoptimized + debuginfo] target(s) in 18.05s
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_return_immediately_when_found ... FAILED
test solution_test::test_small_treasure ... ok
test solution_test::test_small_treasure_2 ... ok
failures:
---- solution_test::test_nothing stdout ----
thread 'solution_test::test_nothing' panicked at tests/solution_test.rs:234:60:
test timeout
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- 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
failures:
solution_test::test_nothing
solution_test::test_return_immediately_when_found
test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.00s
error: test failed, to rerun pass `--test solution_test`
