Решение на Домашно 1 - търсене на съкровища от Мариян Момчилов
Към профила на Мариян Момчилов
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 4 успешни тест(а)
- 1 неуспешни тест(а)
Код
use std::{
collections::HashMap,
sync::mpsc::{channel, Receiver, Sender},
};
#[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 {
sender: Sender<TreasureLoc>,
lane_index: usize,
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
for scan in scanner {
for (i, cell) in scan.cells.iter().enumerate() {
if *cell > 0 {
let result = self.sender.send(TreasureLoc {
lane_index: self.lane_index,
cell_coord: scan.start_coord + i,
value: *cell,
});
match result {
Err(e) => {
println!("{:?} was not send because of error", e.0);
return;
}
_ => {}
}
}
}
}
}
}
pub struct DroneController {
ch: (Option<Sender<TreasureLoc>>, Receiver<TreasureLoc>),
}
impl DroneController {
pub fn new() -> Self {
let ch = channel();
DroneController {
ch: (Some(ch.0), ch.1),
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
Drone {
sender: self.ch.0.as_ref().unwrap().clone(),
lane_index: lane_index,
}
}
pub fn run(&mut self) -> FoundTreasures {
let mut ordered_lanes = Vec::<usize>::new();
let mut best_treasure_locs = HashMap::<usize, TreasureLoc>::new();
let mut accumulated_sum = 0;
self.ch.0.take();
while let Ok(msg) = self.ch.1.recv() {
if msg.value >= 999 {
return FoundTreasures::Big(msg);
}
let crr_best_treasure = best_treasure_locs.get(&msg.lane_index);
if let Some(treasure) = crr_best_treasure {
if treasure.value < msg.value {
accumulated_sum -= treasure.value;
accumulated_sum += msg.value;
best_treasure_locs.insert(msg.lane_index, msg);
if accumulated_sum >= 300 {
break;
}
}
} else {
accumulated_sum += msg.value;
Self::insert_ordered(&mut ordered_lanes, msg.lane_index);
best_treasure_locs.insert(msg.lane_index, msg);
if accumulated_sum >= 300 {
break;
}
}
}
if accumulated_sum < 300 {
return FoundTreasures::Nothing;
}
FoundTreasures::Small(
ordered_lanes
.into_iter()
.map(|i| best_treasure_locs.remove(&i).unwrap())
.collect(),
)
}
fn insert_ordered(v: &mut Vec<usize>, value: usize) {
v.push(value);
let mut iter = v.iter_mut().rev();
let mut last = iter.next();
loop {
if let Some(item) = last.as_mut() {
let mut next = iter.next();
if let Some(item2) = next.as_mut() {
if **item > **item2 {
let tmp = **item;
**item = **item2;
**item2 = tmp;
last = next;
} else {
break;
}
} else {
break;
}
}
}
}
}
Лог от изпълнението
Updating crates.io index
Locking 46 packages to latest compatible versions
Compiling proc-macro2 v1.0.104
Compiling quote v1.0.42
Compiling libc v0.2.178
Compiling unicode-ident v1.0.22
Compiling syn v2.0.111
Compiling futures-core v0.3.31
Compiling futures-sink v0.3.31
Compiling pin-project-lite v0.2.16
Compiling parking_lot_core v0.9.12
Compiling futures-channel v0.3.31
Compiling pin-utils v0.1.0
Compiling scopeguard v1.2.0
Compiling memchr v2.7.6
Compiling cfg-if v1.0.4
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling smallvec v1.15.1
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-12x1v9p/solution)
Finished `test` profile [unoptimized + debuginfo] target(s) in 17.80s
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`
