flowers field done

This commit is contained in:
Rorik Star Platinum 2025-12-07 18:31:05 +03:00
parent 90ff5b2167
commit 99212eb167
2 changed files with 58 additions and 16 deletions

View file

@ -1,5 +1,59 @@
pub fn annotate(garden: &[&str]) -> Vec<String> {
todo!(
"\nAnnotate each square of the given garden with the number of flowers that surround said square (blank if there are no surrounding flowers):\n{garden:#?}\n"
);
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<String> {
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()
}

View file

@ -9,7 +9,6 @@ fn no_rows() {
}
#[test]
#[ignore]
fn no_columns() {
let input = &[""];
let expected = &[""];
@ -18,7 +17,6 @@ fn no_columns() {
}
#[test]
#[ignore]
fn no_flowers() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -35,7 +33,6 @@ fn no_flowers() {
}
#[test]
#[ignore]
fn garden_full_of_flowers() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -52,7 +49,6 @@ fn garden_full_of_flowers() {
}
#[test]
#[ignore]
fn flower_surrounded_by_spaces() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -69,7 +65,6 @@ fn flower_surrounded_by_spaces() {
}
#[test]
#[ignore]
fn space_surrounded_by_flowers() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -86,7 +81,6 @@ fn space_surrounded_by_flowers() {
}
#[test]
#[ignore]
fn horizontal_line() {
let input = &[" * * "];
let expected = &["1*2*1"];
@ -95,7 +89,6 @@ fn horizontal_line() {
}
#[test]
#[ignore]
fn horizontal_line_flowers_at_edges() {
let input = &["* *"];
let expected = &["*1 1*"];
@ -104,7 +97,6 @@ fn horizontal_line_flowers_at_edges() {
}
#[test]
#[ignore]
fn vertical_line() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -125,7 +117,6 @@ fn vertical_line() {
}
#[test]
#[ignore]
fn vertical_line_flowers_at_edges() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -146,7 +137,6 @@ fn vertical_line_flowers_at_edges() {
}
#[test]
#[ignore]
fn cross() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -167,7 +157,6 @@ fn cross() {
}
#[test]
#[ignore]
fn large_garden() {
#[rustfmt::skip]
let (input, expected) = (&[
@ -190,7 +179,6 @@ fn large_garden() {
}
#[test]
#[ignore]
fn multiple_adjacent_flowers() {
let input = &[" ** "];
let expected = &["1**1"];