diff options
author | Federico Igne <git@federicoigne.com> | 2022-07-13 13:18:30 +0100 |
---|---|---|
committer | Federico Igne <git@federicoigne.com> | 2022-07-13 13:18:30 +0100 |
commit | 6875c88c3d5a4b699d588bdf95befaec0c9a18e1 (patch) | |
tree | e544eb76d4f07f5e5bb5aab115074cdd688d57da /rust/minesweeper/src/lib.rs | |
parent | bed0d35b2cccff986e2d4babc1b976b0fe01f62e (diff) | |
download | exercism-master.tar.gz exercism-master.zip |
Diffstat (limited to 'rust/minesweeper/src/lib.rs')
-rw-r--r-- | rust/minesweeper/src/lib.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/rust/minesweeper/src/lib.rs b/rust/minesweeper/src/lib.rs new file mode 100644 index 0000000..febfdb3 --- /dev/null +++ b/rust/minesweeper/src/lib.rs | |||
@@ -0,0 +1,25 @@ | |||
1 | static OFFSETS: &'static[(i32,i32)] = &[ | ||
2 | (-1,-1), (-1, 0), (-1, 1), | ||
3 | ( 0,-1), ( 0, 1), | ||
4 | ( 1,-1), ( 1, 0), ( 1, 1) | ||
5 | ]; | ||
6 | |||
7 | pub fn annotate(minefield: &[&str]) -> Vec<String> { | ||
8 | let height = minefield.len() as i32; | ||
9 | (0..height).map(|i| { | ||
10 | let width = minefield[i as usize].len() as i32; | ||
11 | (0..width).map(|j| { | ||
12 | if minefield[i as usize].as_bytes()[j as usize] == b' ' { | ||
13 | let count = OFFSETS | ||
14 | .iter() | ||
15 | .map(|(oi,oj)| (i+oi,j+oj)) | ||
16 | .filter(|&(i,j)| i >= 0 && i < height && j >= 0 && j < width) | ||
17 | .filter(|&(i,j)| minefield[i as usize].as_bytes()[j as usize] == b'*') | ||
18 | .count(); | ||
19 | if count > 0 { char::from_digit(count as u32,10).unwrap() } else { ' ' } | ||
20 | } else { | ||
21 | '*' | ||
22 | } | ||
23 | }).collect() | ||
24 | }).collect() | ||
25 | } | ||