Решение на Домашно 1 - търсене на съкровища от Деян Делчев

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

Към профила на Деян Делчев

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::{
collections::HashMap,
sync::mpsc::{self, 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],
}
impl<'a> IntoIterator for Scan<'a> {
type Item = (usize, &'a i32);
type IntoIter = std::iter::Scan<
std::slice::Iter<'a, i32>,
usize,
fn(&mut usize, &'a i32) -> Option<Self::Item>,
>;
fn into_iter(self) -> Self::IntoIter {
self.cells.iter().scan(self.start_coord, |coord, cell| {
let curr_coord = coord.clone();
*coord += 1;
Some((curr_coord, cell))
})
}
}
pub struct Drone {
lane_index: usize,
treasure_found_sender: Sender<TreasureLoc>,
termination_receiver: Receiver<()>,
}
impl Drone {
pub fn explore(&mut self, scanner: &mut dyn Iterator<Item = Scan<'_>>) {
for scan in scanner {
for (coord, &cell) in scan {
if let Ok(_) = self.termination_receiver.try_recv() {
return;
}
if cell > 0 {
let _ = self.treasure_found_sender.send(TreasureLoc {
lane_index: self.lane_index,
cell_coord: coord,
value: cell,
});
}
}
}
let _ = self.treasure_found_sender.send(TreasureLoc {
lane_index: self.lane_index,
cell_coord: 0,
value: 0,
});
}
}
pub struct DroneController {
treasure_found_receiver: Receiver<TreasureLoc>,
treasure_found_sender: Sender<TreasureLoc>,
termination_senders: HashMap<usize, Sender<()>>,
}
impl DroneController {
pub fn new() -> Self {
let (treasure_found_sender, treasure_found_receiver) = mpsc::channel();
Self {
treasure_found_receiver,
treasure_found_sender,
termination_senders: HashMap::new(),
}
}
pub fn create_drone(&mut self, lane_index: usize) -> Drone {
let (termination_found_sender, termination_found_receiver) = mpsc::channel();
self.termination_senders
.insert(lane_index, termination_found_sender);
Drone {
lane_index,
treasure_found_sender: self.treasure_found_sender.clone(),
termination_receiver: termination_found_receiver,
}
}
pub fn run(&mut self) -> FoundTreasures {
let mut small_treasure_total_value = 0;
let mut small_treasures = HashMap::new();
let result = loop {
let treasure = self.treasure_found_receiver.recv().unwrap();
match treasure.value {
..=0 => {
self.termination_senders.remove(&treasure.lane_index);
if self.termination_senders.is_empty() {
println!("{small_treasure_total_value}");
break FoundTreasures::Nothing;
}
}
1..999 => {
match small_treasures.get(&treasure.lane_index) {
None => {
small_treasure_total_value += treasure.value;
small_treasures.insert(treasure.lane_index, treasure);
}
Some(old_treasure) => {
if old_treasure.value < treasure.value {
small_treasure_total_value -= old_treasure.value;
small_treasure_total_value += treasure.value;
small_treasures.insert(treasure.lane_index, treasure);
}
}
}
if small_treasure_total_value >= 300 {
break FoundTreasures::Small(small_treasures.into_values().collect());
}
}
999.. => {
break FoundTreasures::Big(treasure);
}
}
};
for sender in self.termination_senders.values() {
let _ = sender.send(());
}
result
}
}

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

Updating crates.io index
     Locking 46 packages to latest compatible versions
   Compiling proc-macro2 v1.0.104
   Compiling unicode-ident v1.0.22
   Compiling libc v0.2.178
   Compiling quote v1.0.42
   Compiling syn v2.0.111
   Compiling parking_lot_core v0.9.12
   Compiling futures-sink v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling cfg-if v1.0.4
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling smallvec v1.15.1
   Compiling scopeguard v1.2.0
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling slab v0.4.11
   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-171s1mk/solution)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 17.83s
     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 ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.10s

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

Деян качи първо решение на 23.12.2025 19:30 (преди около 1 месеца)