From ffe7f7c9aff0467e8bfe13f621946f1a83138a1a Mon Sep 17 00:00:00 2001 From: Rorik Star Platinum Date: Sun, 7 Dec 2025 20:33:47 +0300 Subject: [PATCH] flowers field improved --- rust/flower-field/src/lib.rs | 63 +++++++++-------------------- rust/flower-field/src/lib_first.rs. | 59 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 rust/flower-field/src/lib_first.rs. diff --git a/rust/flower-field/src/lib.rs b/rust/flower-field/src/lib.rs index 7b9bdb1..22984c8 100644 --- a/rust/flower-field/src/lib.rs +++ b/rust/flower-field/src/lib.rs @@ -1,30 +1,22 @@ -pub enum Cell { - Some(u32), - Flower, - None, -} - pub trait NeighbourCounter { - fn count_neighbours(&self, x: usize, y: usize) -> u8; + fn place_cell(&self, x: usize, y: usize) -> char; } impl NeighbourCounter for [&str] { - fn count_neighbours(&self, y: usize, x: usize) -> u8 { - if 0 == self.len() { - return 0; + fn place_cell(&self, x: usize, y: usize) -> char { + if b'*' == self[y].as_bytes()[x] { + return '*'; + }; + let count = (y.saturating_sub(1)..=(y + 1).min(self.len() - 1)) + .flat_map(|ny| { + (x.saturating_sub(1)..=(x + 1).min(self[0].len() - 1)).map(move |nx| (ny, nx)) + }) + .filter(|&(ny, nx)| b'*' == self[ny].as_bytes()[nx]) + .count(); + match count { + 0 => ' ', + c => (b'0' + c as u8) as char, } - let mut count = 0; - for i in y.saturating_sub(1)..=(y + 1).min(self.len() - 1) { - for j in x.saturating_sub(1)..=(x + 1).min(self[0].len() - 1) { - if i == y && j == x { - continue; - } - if b'*' == self[i].as_bytes()[j] { - count += 1; - } - } - } - count } } @@ -32,28 +24,11 @@ pub fn annotate(garden: &[&str]) -> Vec { if 0 == garden.len() { return vec![]; } - garden - .iter() - .enumerate() - .map(|(i, line)| { - let new_line: String = line - .as_bytes() - .iter() - .enumerate() - .map(|(j, &byte)| { - if b'*' == byte { - return '*'; - } - - let count = garden.count_neighbours(i, j); - - match count { - 0 => ' ', - c => (b'0' + c) as char, - } - }) - .collect(); - new_line + (0..garden.len()) + .map(|y| { + (0..garden[y].len()) + .map(|x| garden.place_cell(x, y)) + .collect() }) .collect() } diff --git a/rust/flower-field/src/lib_first.rs. b/rust/flower-field/src/lib_first.rs. new file mode 100644 index 0000000..7b9bdb1 --- /dev/null +++ b/rust/flower-field/src/lib_first.rs. @@ -0,0 +1,59 @@ +pub enum Cell { + Some(u32), + Flower, + None, +} + +pub trait NeighbourCounter { + fn count_neighbours(&self, x: usize, y: usize) -> u8; +} + +impl NeighbourCounter for [&str] { + fn count_neighbours(&self, y: usize, x: usize) -> u8 { + if 0 == self.len() { + return 0; + } + let mut count = 0; + for i in y.saturating_sub(1)..=(y + 1).min(self.len() - 1) { + for j in x.saturating_sub(1)..=(x + 1).min(self[0].len() - 1) { + if i == y && j == x { + continue; + } + if b'*' == self[i].as_bytes()[j] { + count += 1; + } + } + } + count + } +} + +pub fn annotate(garden: &[&str]) -> Vec { + if 0 == garden.len() { + return vec![]; + } + garden + .iter() + .enumerate() + .map(|(i, line)| { + let new_line: String = line + .as_bytes() + .iter() + .enumerate() + .map(|(j, &byte)| { + if b'*' == byte { + return '*'; + } + + let count = garden.count_neighbours(i, j); + + match count { + 0 => ' ', + c => (b'0' + c) as char, + } + }) + .collect(); + new_line + }) + .collect() +}