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

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

Към профила на Ясин Йосифов

Резултати

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

Код

use std::sync::mpsc::{channel, Sender, Receiver};
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
const BIG_TREASURE_VALUE: i32 = 999;
const SMALL_TREASURE_MIN_VALUE: i32 = 300;
#[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 {
done: Arc<RwLock<bool>>,
lane_index: usize,
report: Sender<TreasureLoc>
}
pub struct DroneController {
done: Arc<RwLock<bool>>,
send: Sender<TreasureLoc>,
recv: Receiver<TreasureLoc>,
stats: HashMap<usize, (usize, i32)>,
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
for scan in scanner {
let mut i: usize = 0;
for &c in scan.cells {
let done = self.done.read().unwrap();
if *done == true {
return
};
drop(done);
if c > 0 {
if let Err(_) = self.report.send(TreasureLoc {
lane_index: self.lane_index,
cell_coord: scan.start_coord + i,
value: c
}) {
return
};
};
i += 1;
}
}
}
}
impl DroneController {
pub fn new() -> Self {
let (s, r) = channel();
DroneController {
done: Arc::new(RwLock::new(false)),
send: s,
recv: r,
stats: HashMap::new()
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
if let Some(_) = self.stats.insert(lane_index, (0, 0)) {
panic!("replaced existing drone {}", lane_index);
}
Drone {
done: self.done.clone(),
lane_index: lane_index,
report: self.send.clone()
}
}
pub fn run(&mut self) -> FoundTreasures {
let mut sum: i32 = 0;
let done = self.done.read().unwrap();
if *done == true {
panic!("done running")
};
drop(done);
let found = loop {
match self.recv.recv() {
Ok(loc) => {
if let Some(old) = self.stats.insert(loc.lane_index, (loc.cell_coord, loc.value)) {
if loc.value >= BIG_TREASURE_VALUE {
break FoundTreasures::Big(loc)
} else {
sum -= old.1;
sum += loc.value;
if sum >= SMALL_TREASURE_MIN_VALUE {
break FoundTreasures::Small(
self.stats.iter()
.filter(|(&_, &v)| v.1 > 0)
.map(|(&k, &v)| TreasureLoc {
lane_index: k,
cell_coord: v.0,
value: v.1
})
.collect()
)
}
}
} else {
panic!("logic failure");
}
},
Err(e) => panic!("{:?}", e)
}
};
let mut done = self.done.write().unwrap();
*done = true;
found
}
}

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

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 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 scopeguard v1.2.0
   Compiling memchr v2.7.6
   Compiling smallvec v1.15.1
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   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-6y7g6c/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_big_treasure ... ok
test solution_test::test_return_immediately_when_found ... ok
test solution_test::test_small_treasure ... ok
test solution_test::test_small_treasure_2 ... ok
test solution_test::test_nothing ... FAILED

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


failures:
    solution_test::test_nothing

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 версия и 1 коментар)

Ясин качи първо решение на 23.12.2025 16:51 (преди около 1 месеца)