blob: efca354dec873bf1d86007f95c75a81e2abba9f5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# Minesweeper
Welcome to Minesweeper on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Add the mine counts to a completed Minesweeper board.
Minesweeper is a popular game where the user has to find the mines using
numeric hints that indicate how many mines are directly adjacent
(horizontally, vertically, diagonally) to a square.
In this exercise you have to create some code that counts the number of
mines adjacent to a given empty square and replaces that square with the
count.
The board is a rectangle composed of blank space (' ') characters. A mine
is represented by an asterisk ('\*') character.
If a given space has no adjacent mines at all, leave that square blank.
## Examples
For example you may receive a 5 x 4 board like this (empty spaces are
represented here with the '·' character for display on screen):
```
·*·*·
··*··
··*··
·····
```
And your code will transform it into this:
```
1*3*1
13*31
·2*2·
·111·
```
## Performance Hint
All the inputs and outputs are in ASCII. Rust `String`s and `&str` are utf8,
so while one might expect "Hello".chars() to be simple, it actually has to
check each char to see if it's 1, 2, 3 or 4 `u8`s long. If we know a `&str`
is ASCII then we can call `.as_bytes()` and refer to the underlying data via a `&[u8]` slice.
Iterating over a u8 slice of ASCII is much quicker as there are no codepoints
involved - every ASCII char is one u8 long.
Can you complete the challenge without cloning the input?
## Source
### Created by
- @EduardoBautista
### Contributed to by
- @ashleygwilliams
- @coriolinus
- @cwhakes
- @EduardoBautista
- @efx
- @ErikSchierboom
- @ffflorian
- @IanWhitney
- @kytrinyx
- @lutostag
- @mkantor
- @nfiles
- @petertseng
- @rofrol
- @stringparser
- @workingjubilee
- @xakon
- @ZapAnton
|